Rjs


#1

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


#2

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


#3

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


#4

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')" -%>