but that doesn’t seem the best way to do it. What is
the recommended way to do this?
I don’t know about the ‘recommended’ part (your approach doen’t seem
‘flawed’ to me), but RJS let’s you test for the presence of a DOM
element
using CSS-based selectors.
Assuming you’ve got an element with an id=‘test’, you might try…
page.select “#test”
page[‘test’].visual_effect :highlight
end
page[‘test’].visual_effect :highlight
I don’t know about the ‘recommended’ part (your approach doen’t seem
‘flawed’ to me), but RJS let’s you test for the presence of a DOM
element
using CSS-based selectors.
Assuming you’ve got an element with an id=‘test’, you might try…
page.select “#test”
page[‘test’].visual_effect :highlight
end
That won’t do much, but
page.select("#test").each {|element| element.visual_effect :highlight}
Thanks for the feedback on this guys. It’s all been very useful to
know.
It seems like this is quite a common occurrence (the need to check if
an element exists before doing something to it) - it seems strange
that an RJS helper doesn’t exist for it - resorting to feeding in JS
using page << seems to bit hackish…
I don’t know about the ‘recommended’ part (your approach doen’t seem
‘flawed’ to me), but RJS let’s you test for the presence of a DOM element
using CSS-based selectors.
Assuming you’ve got an element with an id=‘test’, you might try…
page.select “#test”
page[‘test’].visual_effect :highlight
end
Sigh. that will work (and is basically the same as what I wrote) as
long as you add the ‘do’ that I’m sure you meant to write.
It seems like this is quite a common occurrence (the need to check if
an element exists before doing something to it) - it seems strange
that an RJS helper doesn’t exist for it - resorting to feeding in JS
using page << seems to bit hackish…
An RJS helper does exist. The ‘select’. Thanks to Fred, we now know it
just needs to be used as he recommends: in a block rather than an ‘if
then
else’ His statement re: using page << wasn’t a recommendation; just a
‘if
that’s how you what to do it, this is the syntax you’ll have to use’.
I’d
use his earlier (and better tested than mine) recommend solution.
----- Original Message -----
From: “Frederick C.” [email protected]
To: “Ruby on Rails: Talk” [email protected]
Sent: Wednesday, June 11, 2008 3:10 PM
Subject: [Rails] Re: Some RJS Help
Oops ;-p You’re right. That should have been…
if page.select “#test”
Thanks for the feedback on this guys. It’s all been very useful to
know.
It seems like this is quite a common occurrence (the need to check if
an element exists before doing something to it) - it seems strange
that an RJS helper doesn’t exist for it - resorting to feeding in JS
using page << seems to bit hackish…
if you read the blog post I linked to there is a plugin with helpers
for this. At a basic level I’d say large amounts of complicated
javascript in rjs isn’t really a great idea.
Fred
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.