# Rjs

**URL:** <https://www.ruby-forum.com/t/rjs/85688>\
**Category:** Rails\
**Created:** [February 15, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/rjs/85688 "2007-02-15T16:02:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/rjs/85688/1 "2007-02-15T16:02:23Z")

</div>

I am trying to write my first RJS script to uncheck all radio buttons in  
a div #network\_radio\_btns

page.select(’#network\_radio\_btns input’).each do |element|  
element.set\_attribute(‘checked’, ‘false’)  
end

but this doesn’t work… being converted to js

$$("#network\_radio\_btns input").each(function(value, index) {  
value.setAttribute(“checked”, “false”);  
});

I tested the loop : alert(index) and I can see all indexes but …  
how can I change an option ?.. not the value

thnaks for your help

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/rjs/85688/2 "2007-02-15T17:21:35Z")

</div>

Hi Kad,  
Kad K. wrote:

> I am trying to write my first RJS script to uncheck  
> all radio buttons in a div #network\_radio\_btns
> 
> page.select(’#network\_radio\_btns input’).each do |element|  
> element.set\_attribute(‘checked’, ‘false’)  
> end

> I tested the loop : alert(index) and I can see all indexes but …  
> how can I change an option ?.. not the value

I’d probably just render a new partial rather then trying to reset the  
button. If you really need to go this route, I think the visible  
properties  
of a radio button depend on the value being nil for unchecked. So you  
might  
need to add

element.set\_attribute(‘value’, nil)

I haven’t tested it though.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/rjs/85688/3 "2007-02-15T18:26:33Z")

</div>

Bill W. wrote:

> Hi Kad,  
> Kad K. wrote:
> 
> > I am trying to write my first RJS script to uncheck  
> > all radio buttons in a div #network\_radio\_btns
> > 
> > page.select(’#network\_radio\_btns input’).each do |element|  
> > element.set\_attribute(‘checked’, ‘false’)  
> > end
> 
> > I tested the loop : alert(index) and I can see all indexes but …  
> > how can I change an option ?.. not the value
> 
> I’d probably just render a new partial rather then trying to reset the  
> button. If you really need to go this route, I think the visible  
> properties  
> of a radio button depend on the value being nil for unchecked. So you  
> might  
> need to add
> 
> element.set\_attribute(‘value’, nil)
> 
> I haven’t tested it though.
> 
> hth,  
> Bill

thanks Bill

You’re right… that’s what I going to do…  
it was a quizz for me trying to understand how to use JSCollectionProxy  
in RJS… (got teh PDF from Cody F. but… there is no example… and  
I am not a JS guru…

whatever I found also he solution using RJS : use plain JS…  
page.select(’#network\_radio\_btns input’).each do |index|  
page \<\< “document.display\_networkForm.network\_group[index].checked =  
false”  
end

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [February 15, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/rjs/85688/4 "2007-02-15T19:21:47Z")

</div>

maybe i’m missing something, but why would you need to do this using  
rjs? from your example, you’re making a call to the server, which is  
returning javascript to uncheck all the checkboxes in a div. why not  
just use straight javascript and skip the server altogether?

assuming all the checkboxes are in a form element, you can do  
something like this (requires prototype):

... \<%= link\_to\_function "CHECK ALL", "check\_all('my\_form')" -%\> \<%= link\_to\_function "UNCHECK ALL", "uncheck\_all('my\_form')" -%\>
