Forum: Ruby on Rails RJS

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
RJS
Kad K. (Guest)
on 2007-02-15 17:02
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
Bill W. (Guest)
on 2007-02-15 18:21
(Received via mailing list)
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
Kad K. (Guest)
on 2007-02-15 19:26
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
Chris H. (Guest)
on 2007-02-15 20:21
(Received via mailing list)
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):

<form id="my_form">
...
</form>
<%= link_to_function "CHECK ALL", "check_all('my_form')" -%>
<%= link_to_function "UNCHECK ALL", "uncheck_all('my_form')" -%>

<script type="text/javascript">
function check_all(form) {
  Form.getInputs(form, 'checkbox').each( function(cb) {
                                                        cb.checked =
true;
                                                       });
}

function uncheck_all(form) {
  Form.getInputs(form, 'checkbox').each( function(cb) {
                                                        cb.checked =
false;
                                                       });
}
</script>
This topic is locked and can not be replied to.