# How to access element attributes using RJS?

**URL:** <https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616>\
**Category:** Rails\
**Created:** [July 16, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616 "2006-07-16T22:32:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/1 "2006-07-16T22:32:39Z")

</div>

For example, in the following block:

page.select(‘.foo’).each do |element|  
…  
end

How would I access, just for example, the id attribute of each element  
in  
the collection?

I’ve randomly tried element.id and element.attributes[‘id’], but neither  
of  
those has worked. I’ve also been unsuccessful in finding any  
documentation  
on this, but it seems like such a basic task.

## Thanks! DW

View this message in context:  
[http://www.nabble.com/How-to-access-element-attributes-using-RJS--tf1951711.html#a5352195](http://www.nabble.com/How-to-access-element-attributes-using-RJS--tf1951711.html#a5352195)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/2 "2006-07-19T15:52:47Z")

</div>

dw wrote:

> those has worked. I’ve also been unsuccessful in finding any documentation  
> on this, but it seems like such a basic task.
> 
> Thanks!  
> DW

I’m not sure you can at the moment. I’ve been struggling to do a  
conditional in my RJS template, and finally found a post saying that you  
couldn’t do that because you couldn’t get at the value of the select…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/3 "2006-07-19T16:08:23Z")

</div>

In the examples I’ve seen the each enumerator block has two variables

page.select('.foo).each do |value, index|

```
page << 'alert(value.innerHTML);'

```

end

Also, value is used instead of element. Not sure how significant that is  
but  
give it a whirl.

Regards,  
John

----- Original Message -----  
From: “Chris T” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, July 19, 2006 2:52 PM  
Subject: Re: [Rails] How to access element attributes using RJS?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/4 "2006-07-19T16:17:30Z")

</div>

A little off topic but… in a view file for lisiting links the logic  
goes something like:

\<% if @links.size == 0 %\>

No links...

\<% else %\> ## for link in @link .... \<% end %\>

I have a form on the same page used tro create a new link. In create.rjs  
I’m looking for a way to get rid of page[‘no-links’] only if it exists.  
I’ve tried

page \<\< if($(‘no-links’)) { page[‘no-links’].remove }

which is obviously wrong. Can someone tell me what to put in create.rjs  
to remove #no-links if it exists in the DOM? Many thanks…

Greg

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/5 "2006-07-19T16:40:09Z")

</div>

Try this, it assumes your controller increments the links before the rjs  
file is processed

page[:no-links].remove if @links.size == 1

----- Original Message -----  
From: “Greg” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, July 19, 2006 3:17 PM  
Subject: [Rails] Re: How to access element attributes using RJS?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2006, 6:24am UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/6 "2006-07-20T06:24:19Z")

</div>

John W. wrote:

> Try this, it assumes your controller increments the links before the rjs  
> file is processed

Thanks. I’ve attempted the following but it didn’t work:

# links\_controller.rb:

def create  
@link = Link.new(params[:link])  
@saved = @link.save  
@num\_links = Link.find(:all).size  
if request.xhr?  
page[:listing-status].replace\_html “You’ve created #{@num\_links}  
links in #{@num\_cats} categories”  
page[:no-links].remove if @num\_links == 1 && page[:no-links]  
return  
end  
if @saved  
flash[:message] = ‘Link was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

# create.rjs

if @saved  
page.insert\_html :after, “category-#{@link.category\_id}”, :partial =\>  
‘link’, :object =\> @link  
page.visual\_effect :highlight, “link-#{@link.id}”  
page.replace\_html ‘new\_link\_error’, ‘’  
page[‘link\_url’].value = ‘http://’  
page[‘link\_name’].value = ‘’  
page[‘link\_description’].value = ‘’  
else # not saved, present error messages  
page.replace\_html ‘new\_link\_error’, error\_messages\_for(‘link’)  
page.visual\_effect :appear, ‘new\_link\_error’, :duration =\> 0.5  
page.visual\_effect :highlight, ‘new\_link\_error’, :duration =\> 0.5  
end

#Error in links\_controller#create  
NameError: undefined local variable or method `page’

Guess you can’t use page[] in the controller…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 20, 2006, 6:40am UTC](https://www.ruby-forum.com/t/how-to-access-element-attributes-using-rjs/98616/7 "2006-07-20T06:40:15Z")

</div>

Greg wrote:

> Guess you can’t use page[] in the controller…

Certainly not true. This works:

#from links\_controller.rb  
if request.xhr?  
render :update do |page|  
if @saved  
page[‘listing-status’].replace\_html “You’ve created  
#{num\_links} links in #{num\_cats} categories”  
page[‘no-links’].remove if @num\_links == 1 && page[‘no-links’]  
page.insert\_html :after, “category-#{@link.category\_id}”,  
:partial =\> ‘link’, :object =\> @link  
page.visual\_effect :highlight, “link-#{@link.id}”  
page.replace\_html ‘new\_link\_error’, ‘’  
page[‘link\_url’].value = ‘http://’  
page[‘link\_name’].value = ‘’  
page[‘link\_description’].value = ‘’  
else # not saved, present error messages  
page.replace\_html ‘new\_link\_error’, error\_messages\_for(‘link’)  
page.visual\_effect :appear, ‘new\_link\_error’, :duration =\> 0.5  
page.visual\_effect :highlight, ‘new\_link\_error’, :duration =\>  
0.5  
end  
end #end update  
return  
end

…I smell a helper! Cheers.

Greg
