nanadjei2's avatar

JQuery option click even not working

i want to write a logic where a user select an option, and a textbox shows. But it does not work. This is the HTML

  <select class="form-control" id="executive" name="national_executive" required>
      <option class="active">I am ...</option>
      <option value="0">not a national executive</option>
      <option id="nationalExecutive" value="1">a executive</option>
    </select>
        <div class="textarea nationalExecutive">
          <textarea class="form-control" name="name" rows="3" cols="12" placeholder="Rectify Your Position"></textarea>
        </div>

This is my script

  <script type="text/javascript">
   $(document).ready(function(){
      $('.nationalExecutive').hide();
      $('#nationalExecutive').click(function() {
        $('.nationalExecutive').show();
      });
    }); 
 </script>
0 likes
4 replies
tomopongrac's avatar

@nanadjei2

You need change event

https://api.jquery.com/change/

and you have wrong selector in code

Here is javascript

<script>
    $(document).ready(function(){
        $('.nationalExecutive').hide();
        $('#executive').change(function() {
            $('.nationalExecutive').show();
        });
    });
</script>
<select class="form-control" id="executive" name="national_executive" required>
    <option class="active">Choose your position...</option>
    <option>I am ...</option>
    <option value="0">not a national executive</option>
    <option id="nationalExecutive" value="1">a executive</option>
</select>
<div class="textarea nationalExecutive">
    <textarea class="form-control" name="name" rows="3" cols="12" placeholder="Rectify Your Position"></textarea>
</div>
nanadjei2's avatar

@tomo_pongrac , i want it in a way that when the user selects <option id="nationalExecutive" value="1">a executive</option> the textbox will show.

tomopongrac's avatar
Level 51

@nanadjei2

Try

<script>
    $(document).ready(function(){
        $('.nationalExecutive').hide();
        $('#executive').change(function() {
            if ($(this).val() == 1) {
                $('.nationalExecutive').show();
            }
            else
            {
                $('.nationalExecutive').hide();
            }
        });
    });
</script>
1 like

Please or to participate in this conversation.