-2

I have a drop down menu, I want to show a div having another drop down menu when I select an option in first drop down.

For this, I have used onClick function on every option tag. Every option has a different div. the problem is; It isn't showing me the div when I select an option.

Here is my code:

<select name="first">

  <option selected; value="" disabled="disabled">Select an Option</option> 

  <option value="all">Select All</option>   
  <option value="Sno">Sno</option>
  <option value="name" onclick="showMe('namediv',this)" onclick="ckChnage(this)">Name</option>
  <option value="course" onclick="showMe('coursediv',this)" >Mauza</option>
  </select>
  <div id="namediv" style="display: none";>
    <select name="firstres" id="firstres"><option style="display:none;" selected; value="">---Select an option---</option>
<?php 
   $sql="SELECT * FROM table ";     
   $result = mysql_query($sql);

   while ($row = mysql_fetch_array($result)) 
   {
      echo "<option  value=' " . $row['Sno'] ."'>" . $row['name'] ."</option>";
   }
?>
    </select>
</div>
Pradeep
  • 9,667
  • 13
  • 27
  • 34
DevJ
  • 27
  • 1
  • 9

3 Answers3

0

for selecet you have to use onchang

Here you can learn about it:https://www.w3schools.com/jsref/event_onchange.asp

function myFunction() {
    var x = document.getElementById("select").value;
       if(x == "course"){
        document.getElementById('coursediv').style.display="block";
    } 
}
<select name="first" onchange="myFunction()" id="select">

  <option selected; value="" disabled="disabled">Select an Option</option> 

  <option value="all">Select All</option>   
  <option value="Sno">Sno</option>
  <option value="name">Name</option>
  <option value="course">Mauza</option>
  </select>
<div id="coursediv" style="display:none">I am coursediv dive</div>
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
0

onclick does not work on option tag, you might want to use onchange event on select tag

Edit: Since you wish to hide all other divs, you can first hide all div, then display the selected div

function showMe(element){
  if ( element.value==='all' ) {
    document.getElementById( 'Snodiv'    ).style.display = 'block';
    document.getElementById( 'namediv'   ).style.display = 'block';
    document.getElementById( 'coursediv' ).style.display = 'block';
  } else {
    document.getElementById( 'Snodiv'    ).style.display = 'none';
    document.getElementById( 'namediv'   ).style.display = 'none';
    document.getElementById( 'coursediv' ).style.display = 'none';
    
    var div = document.getElementById( element.value + "div" );
  
    if ( div ) {
      div.style.display = 'block';
    }
  }
}
<select name="first" onchange="showMe(this)">

  <option selected value="" disabled="disabled">
    Select an Option
  </option> 
  <option value="all">Select All</option>   
  <option value="Sno">Sno</option>
  <option value="name">Name</option>
  <option value="course">Mauza</option>
</select>

<div id="Snodiv" style="display: none";>
    Sno
    <select name="firstres" id="firstres">
      <option selected value="">
        ---Select an option---
      </option>
      <option value="some-option">Some option</option>
    </select>
</div>

<div id="namediv" style="display: none";>
    Name
    <select name="firstres" id="firstres">
      <option selected value="">
        ---Select an option---
      </option>
      <option value="some-option">Some option</option>
    </select>
</div>

<div id="coursediv" style="display: none";>
    Mauza
    <select name="firstres" id="firstres">
      <option selected value="">
        ---Select an option---
      </option>
      <option value="some-option">Some option</option>
    </select>
</div>
Thum Choon Tat
  • 3,084
  • 1
  • 22
  • 24
  • That's working. Can u please help me with one more thing? I want to hide all the other divs and only show the one whose option is selected. Thanks alot – DevJ May 09 '18 at 06:52
0

You can't achieve what you want using onclick as you have a drop-down element.

You need to use onChange not onClick since you need to check when the "drop-down" was "changed".

Refer here for further reading about onChange.

You need to change your code to this:

<select name="first" id="first_select">
  <option value="" selected>Select an Option</option> 
  <option value="all">Select All</option>   
  <option value="Sno">Sno</option>
  <option value="name">Name</option>
  <option value="course">Mauza</option>
</select>

I added an ID as a reference for the script. You can implement this in many ways I just prefer it this way.

<script>
$(function(){
    $("#first_select").change(function(){
        if(this.value == "course"){
            showMe('coursediv',this);
        } 
    });
});
</script>

We check when your first drop-down element is changed and when it does we trigger your showMe function. In my answer, I checked first if the selected element is "course" and if it is then we show the "coursediv". You can add your other option there as well to display what you need it to display.

hungrykoala
  • 1,083
  • 1
  • 13
  • 28