Jump to content

Drop down menu appear


the_oliver

Recommended Posts

Hi,

 

Appolojies if this is not done through Javascript...

 

I have a drop-down menu, and if a particular option is chosen i need another to apper.  For any other options the second drop down box  should remain hidden.  Could any one suggest how i would best go about this or perhaps point me in the direction of a tutorial?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/163275-drop-down-menu-appear/
Share on other sites

Use an onchange function.

 

Example -

<select id='e' onchange='foo(this);'>
<option value='a'>a</option>
<option value='b'>b</option>
<option value='c'>c</option>
<option value='d'>d</option>
<option value='e'>e</option>
</select>

<select id='k' style='display: none;'>
<option value='a'>a</option>
<option value='b'>b</option>
<option value='c'>c</option>
<option value='d'>d</option>
<option value='e'>e</option>
</select>

<script type='text/javascript'>
function foo (e) {
     var idx = e.options.selectedIndex;
     if (e.options[idx].value === 'e') document.getElementById('k').style.display = '';
}
</script>

That IF statement needs an else - otherwise the display will never return to hidden. I'd use the ternary operator in this situation.

 

function foo (e) {
     var idx = e.options.selectedIndex;
     document.getElementById('k').style.display = (e.options[idx].value === 'e')  '' : 'hidden';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.