I tried several ways but all are failing.
views:
571answers:
5
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
2009-03-20 19:06:46
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
2009-03-20 19:21:26
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
2009-03-20 23:07:03
+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
2009-03-21 18:05:47
A:
İ. Emre Kutlu
2009-03-26 13:41:19
A:
I answered this one over here: http://stackoverflow.com/questions/630637/rjs-check-for-existing-page-element
inkdeep
2009-05-23 23:34:12