Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.