0

I have a select with some options in HTML and what I am trying to do is for every different selected option I want to "Unhide" or display another set of options. Hopefully that makes sense. So I have a drop down options like 1, 2 if user select option, i.e. 1, I want to display another drop down menu.

The issue is that upon selection the 2nd drop down menu does not show at all.

The HTML:

  <select id="workshop" name="workshop" onclick="return test();">
                <option value="">Please select a Workshop</option>
                <option value="f">1</option>
                <option value="b">2</option>
            </select>

            <select id="date" name="date" style="display: none">
                <option value="">Please select a date</option>
                <option value="01/01/01">01/01/01 at 6.30pm</option>
            </select>

JavaScript:

function test(){
   if(document.getElementById('workshop').value == 'f'){
      document.getElementById('date').style.display = 'block';
    }
    else{
      document.getElementById('date').style.display = '';
    }
}

5 Answers 5

2

First of all Use onchange event instead of using onclick. because onchange event occurs when the value of an element has been changed(As your requirement).

Try with this code:

function test(){
   if(document.getElementById('workshop').value == 'f'){
      document.getElementById('date').style.display = 'block';
    }
    else{
      document.getElementById('date').style.display = 'none';
    }
}

Try in Js FIddle

Sign up to request clarification or add additional context in comments.

Comments

1
function test(){
    if(document.getElementById('workshop').value == 'Forex'){
    document.getElementById('date').style.display = 'inline-block';//try this
}else{
    document.getElementById('date').style.display = 'none';
}

1 Comment

Hello, I tried your solution to this problem by replacing onclick with onchange but still the problems remains the same.
1

Do not use the onclick event. Use onchange:

<select id="workshop" name="workshop" onchange="return test();">
   <option value="">Please select a Workshop</option>
   <option value="f">1</option>
   <option value="b">2</option>
</select>

<select id="date" name="date" style="display: none">
   <option value="">Please select a date</option>
   <option value="01/01/01">01/01/01 at 6.30pm</option>
</select>

Onclick works well with buttons, but any kind of input reacts better on change. I bet it shows up the second time you click on the first select.

PS: Text inputs and textareas work even better when checking onkeyup or onkeydown.

http://www.w3schools.com/jsref/event_onchange.asp

1 Comment

Hello, I tried your solution to this problem by replacing onclick with onchange but still the problems remains the same.
1

Use onchange

HTML

<select id="leave" onchange="test()">
  <option value="">Please select a Workshop</option>
  <option value="f">1</option>
  <option value="b">2</option>
</select>

JavaScript

    function test()
    {
     if(document.getElementById('workshop').value == 'f')
     {
       document.getElementById('date').style.display = 'block';
     }
    else
    {
      document.getElementById('date').style.display = '';
    }
   }

Comments

1

OKkkkk Hello people I found the issue, What happens is that I had my HTML & JS files both opened form different directories :D So the changes I was making to my JS file did not affect the HTML file which i was oppening in the browser. Really sorry for my mistake guys everyone up 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.