Topic: Hide/unhide using prototype

I'm sorry if this is a stupid question but I'm totally stumped.

I have a view where I have 2 radio_buttons with corresponding selection box below each.
Initially I want the user only to see the two radio_buttons next to the google map I'm showing. When the user selects one of the radio_buttons  the corresponding select box is shown.

This must all happen without the action having to go to the controller.

Both select boxes however as shown when the page loads and in IE6 I get an error mesg

Last edited by JdeJong (2007-01-21 05:00:56)

Re: Hide/unhide using prototype

I don't think observe_field is the way to go. This is designed to work with AJAX/server-side requests and responses. Instead I recommend overriding the "onchange" attribute of the radio button. You can then toggle the visibility of the select field directly like this:

<%= f.radio_button :filter, :garage, :onchange => "Element.toggle('garage_map_name')" %>
<%= f.radio_button :filter, :make, :onchange => "Element.toggle('garage_map_car_make')" %>

Untested. See if that works for you.

Railscasts - Free Ruby on Rails Screencasts

Re: Hide/unhide using prototype

Thanks Ryan, it works, but there is one problem. Selecting a radio_button does not show the select immediately, it only does so when you select the other radio_button.

Not quite what I want to accomplish. I thought I was being clever by changing the onchange to onclick; but then you need to physically select the radio_button again to hide the corresponding select box.

Then I decided to add both onchange and onclick but boy that really confused matters.

I'll keep you posted, at least you pointed me in the right direction smile

Re: Hide/unhide using prototype

You could try onclick and send two commands, one to show the correct select box and one to hide the other:

:onclick => "'garage_map_name'); Element.hide('garage_map_car_make');"

The reason I was avoiding onclick is because I wasn't sure if it was fully accessible to users who don't use the mouse.

Railscasts - Free Ruby on Rails Screencasts

Re: Hide/unhide using prototype

Works like a dream Ryan. Thanks !!!!! smile