# Accessing DOM node information via RJS

**URL:** <https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066>\
**Category:** Rails\
**Created:** [October 5, 2006, 4:51am UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066 "2006-10-05T04:51:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@vishal](https://www.ruby-forum.com/u/vishal)\
**Post date:** [October 5, 2006, 4:51am UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066/1 "2006-10-05T04:51:46Z")

</div>

I have been pulling my hair out for way too long on this…and it seems  
so trivial! The following does not work:

if page[‘test’].className == “whatever”  
page[‘test’].className = “whatever2”  
end

Now that I write this post, I realize why it doesn’t work - className is  
a value in the Javascript code - not in Rails. But, is there any way to  
access the className value in the IF statement?

Thanks,  
Vishal

---

<div class="post-metadata">

**Author:** ![vishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@vishal](https://www.ruby-forum.com/u/vishal)\
**Post date:** [October 5, 2006, 4:59am UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066/2 "2006-10-05T04:59:45Z")

</div>

> I have been pulling my hair out for way too long on this…and it seems  
> so trivial! The following does not work:
> 
> if page[‘test’].className == “whatever”  
> page[‘test’].className = “whatever2”  
> end
> 
> Now that I write this post, I realize why it doesn’t work - className is  
> a value in the Javascript code - not in Rails. But, is there any way to  
> access the className value in the IF statement?

select maybe?

[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M000430](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M000430)

* * *

select(pattern)

Returns a collection reference by finding it through a CSS pattern in  
the  
DOM. This collection can then be used for further method calls.  
Examples:

page.select(‘p’) # =\> $$(‘p’);  
page.select(‘p.welcome b’).first # =\> $$(‘p.welcome b’).first();  
page.select(‘p.welcome b’).first.hide # =\> $$(‘p.welcome  
b’).first().hide();

You can also use prototype enumerations with the collection. Observe:

page.select(‘#items li’).each do |value|  
value.hide  
end

# =\> $$(‘#items li’).each(function(value) { value.hide(); });

Though you can call the block param anything you want, they are always  
rendered in the javascript as .value, index… Other enumerations, like  
collect() return the last statement:

page.select(‘#items li’).collect(‘hidden’) do |item|  
item.hide  
end

# =\> var hidden = $$(‘#items li’).collect(function(value, index) {

return value.hide(); });

---

<div class="post-metadata">

**Author:** ![vishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@vishal](https://www.ruby-forum.com/u/vishal)\
**Post date:** [October 5, 2006, 5:23am UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066/3 "2006-10-05T05:23:16Z")

</div>

Nope…After perusing through some message boards, it seems that RJS  
does not currently have the ability to use Ruby conditional logic. =(

---

<div class="post-metadata">

**Author:** ![vishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@vishal](https://www.ruby-forum.com/u/vishal)\
**Post date:** [October 5, 2006, 5:59am UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066/4 "2006-10-05T05:59:48Z")

</div>

Are you trying to update the style class of a specific node or all of  
the  
nodes containing a particular class?

Is the RJS returned from a specific AJAX function link from the node you  
are  
interested in? You could use the :with attribute to pass in the style  
class  
name if this is the case.

On 10/4/06, Vishal A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Nope…After perusing through some message boards, it seems that RJS  
> does not currently have the ability to use Ruby conditional logic. =(
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Thanks,  
-Steve  
[http://www.stevelongdo.com](http://www.stevelongdo.com)

---

<div class="post-metadata">

**Author:** ![vishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@vishal](https://www.ruby-forum.com/u/vishal)\
**Post date:** [October 5, 2006, 10:54am UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066/5 "2006-10-05T10:54:16Z")

</div>

Vishal A. wrote:

> I have been pulling my hair out for way too long on this…and it seems  
> so trivial! The following does not work:
> 
> if page[‘test’].className == “whatever”  
> page[‘test’].className = “whatever2”  
> end
> 
> Now that I write this post, I realize why it doesn’t work - className is  
> a value in the Javascript code - not in Rails. But, is there any way to  
> access the className value in the IF statement?

Yeah, there’s a fundamental misunderstanding going on here. When you  
write an RJS template, you’re not writing Ruby that communicates ‘live’  
with Javascript on the web browser. The entire RJS template is  
converted to Javascript on the server, and is then sent in one big lump  
to the web browser, where it is run.

Therefore, you cannot do anything in RJS that involves ‘reading’  
something about the state of the browser or document. Or in other  
words, the entire RJS template has to make sense without any knowledge  
of what’s going on in the browser/document.

I’d just drop into Javascript for this kind of thing.

Chris

---

<div class="post-metadata">

**Author:** ![vishal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/49beb7/32.png) [@vishal](https://www.ruby-forum.com/u/vishal)\
**Post date:** [October 5, 2006, 1:16pm UTC](https://www.ruby-forum.com/t/accessing-dom-node-information-via-rjs/71066/6 "2006-10-05T13:16:31Z")

</div>

On 5 October 2006 12:51, Chris M. wrote:

> > access the className value in the IF statement?  
> > of what’s going on in the browser/document.
> 
> I’d just drop into Javascript for this kind of thing.  
> Yet, there is a solution. I had the very same problems and have written  
> a  
> simple plugin that allows something like this:

page.if page[‘test’].hasClassName(‘whatever’) do  
page[‘test’].classNames.set(‘whatever’)  
end

You can install it with this command:  
./script/plugin install  
svn://rubyforge.org/var/svn/js-if-blocks/trunk/js-if-blocks
