I tried several ways but all are failing.
Asked
Active
Viewed 2,669 times
0
-
2It would help if you told us which ways you tried and failed. – Pablo Fernandez Mar 20 '09 at 19:09
4 Answers
3
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
- 14,394
- 6
- 41
- 36
0
Actually, I couldn't get the
if page[:element]
# code here
end
to work. Instead, I ended up using
page << "if( $('element') ) {"
# code here
page << "}"

James Kennedy
- 21
- 3
-1
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

dstnbrkr
- 4,305
- 22
- 23
-
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 Mar 20 '09 at 19:21
-1
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
- 20,047
- 8
- 38
- 56