views:

571

answers:

5

I tried several ways but all are failing.

A: 

You can use the [ ] method of the JavascriptGenerator to find an element like this:

page['theElementId']

Here's a link to the details:

Module ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods

dbarker
Uhmm... have you tried it??? What happens if the element is not there? What you described is a way to select the element ... I am looking for a way to detect existence. The documentation online is not very clear.
fooledbyprimes
A: 

You could use what dbarker said like this:

 if page['theElementId'].nil?
       # then have you logic here if the element does not exist
 else
       # if the element does exist
 end
vrish88
+2  A: 

As dbarker mentioned, you can use page['theElementID'] to test whether a specific HTML element exists based on its ID.

If your target element doesn't have an ID attribute, you can also check for it with a CSS selector, including class names. For example:

if page.select('div.comment').any?
    # Logic here if there is at least one comment
else
    # Logic for no comments
end

Documentation on page.select: http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001632

Ron DeVera
A: 

I answered this one over here: http://stackoverflow.com/questions/630637/rjs-check-for-existing-page-element

inkdeep