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

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:
http://www.webdeveloper.com/forum/showthread.php?t=122975

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});
});

  1. 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.

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’);}}”}>

Car Bike Motor
Showing Car
Showing Bike
Showing Motor

Regards,

Salil

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

On 20 feb, 15:13, Robert W. [email protected]