user1906399
user1906399

Reputation: 771

Why is checkbox with jQuery is not working?

Neither .prop('checked') nor .is(':checked') is working.

if($("#isAgeSelected").is(':checked')){
   $("#txtAge").show();
}else{
   $("#txtAge").hide();
}

Html is:

<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">Age is something</div>

JSFiddle

Upvotes: 0

Views: 78

Answers (2)

Michał
Michał

Reputation: 2494

You need to listen to change event:

$("#isAgeSelected").change(function(){
  if($("#isAgeSelected").is(':checked')){
     $("#txtAge").show();
  }else{
     $("#txtAge").hide();
  }
});

Upvotes: 2

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

Updated Code check jsFiddle

HTML

 <input class="isAgeSelected" type="checkbox" name="isAgeSelected" value="1" />
<div id="txtAge">Age is something</div>

jQuery

$("#txtAge").hide();
$(".isAgeSelected").click(function() {
    if($(this).is(":checked")) {
        $("#txtAge").show();
    } else {
        $("#txtAge").hide();
    }
});

Upvotes: 1

Related Questions