0

Hi I attaching a part of my code which should hide the textbox and when female is selected it should show it. But this is not working

$(document).ready(function() {
  $('input[name="gender"]').click(function() {
    var value = $(this).val();
    if( $value == "male")
    {
      $('#address').hide();
    }
    else{
      $('#address').show();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style>
.error {color: #FF0000;}
</style>

Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
    <div name="address" id="address">
   <textarea name="address" id="address" rows="5" cols="40"></textarea>
 </div>
  <br><br>

Please help. Thanks

SLePort
  • 15,211
  • 3
  • 34
  • 44
Ankky
  • 47
  • 2
  • 12

1 Answers1

2

You set value variable but wrote $value in your comparison test.

Try this:

$(document).ready(function() {
  $('input[name="gender"]').click(function() 
                                  {
    var value = $(this).val();
    if( value == "male")
    {
      $('#address').hide();
    }
    else{
      $('#address').show();
    }


  });
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style>
.error {color: #FF0000;}
</style>
Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
    <div name="address" id="address">
   <textarea name="address" id="address" rows="5" cols="40"></textarea>
 </div>
  <br><br>

Edit:

As pointed out by in comments, your snippet did not include the jquery library, so it could not work. Make sure it's present in your code. eg:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
SLePort
  • 15,211
  • 3
  • 34
  • 44
  • Ankky, make sure you also add a reference to jQuery. – Jacob Linney Nov 17 '16 at 19:57
  • @Ankky, JacobLinney is right... check for the jQuery inclusion. The snippets works for me. If it does'nt for you, please copy/paste errors from snippet console. – SLePort Nov 17 '16 at 20:06