show/hide divs according to selection box

hi all,

I have 3 divs (with id) and according to a selection made in a selection box I want to show the corresponding div and hide the 2 other divs

eg:

selectionbox - car - bike - motor

<div id="car"></div> <div id="bike"></div> <div id="motor"></div>

So when car is selected div car should bes hown and the other 2 should be hidden.

How can this best be done?

Thanks Stijn

plain old javascript. if you don't know how to do it, google helps:

Tarscher wrote:

So when car is selected div car should bes hown and the other 2 should be hidden.

How can this best be done?

Thanks Stijn

You can do this using the Prototype library included as the default for Rails, or jQuery if you prefer that.

1. Attached an observer to the selection box after the DOM is ready

Event.observe(window, 'load', function() {   $('select_box').observe('change', selectBoxHandler}); });

2. Use AJAX to update the DOM

function selectBoxHandler() {   switch $F('select_box') {   case 'car':     $('car').show();     $('bike').hide();     $('motor').hide();     break;   case 'bike':     $('bike').show();     $('car').hide();     $('motor').hide();   case 'motor':     $('motor').show();     $('car').hide();     $('bike').hide();     break;   } }

Note: This is just "off-the-cuff" code, but is should show the gist of it. Also this can be accomplished using the Rails Prototype helpers, but I'll leave that as an exercise for you.

Thanks, that helped me a lot.

I would like to set car as the default selection. I can use the :default => in options_for_select but that dooesn't seem to trigger the observer.

I tried to add Event.observe(window, 'load', function() {   $F('game_type').value = 'car' });

after the selection box observer but it doesn't set the car default.

Someoene has an idea?

Thanks

You can try this also .......

<select name="timesheet[filter]" id="filter" onchange="if (value=='car') {Element.show('car');Element.hide('bike');Element.hide('motor');} else {if {(value=='bike') {Element.show('bike');Element.hide('car');Element.hide('motor');}else {Element.show('motor');Element.hide('car');Element.hide('bike');}}"}>    <option value="car" >Car</option>    <option value="bike" >Bike</option>    <option value="motor" >Motor</option>

</select>

<div id="car">Showing Car</div> <div id="bike">Showing Bike</div> <div id="motor">Showing Motor</div>

Regards,

Salil