5196

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is checked, then I need to show a textbox to enter age, else hide the textbox.

But the following code returns false by default:

if ($('#isAgeSelected').attr('checked')) {
  $("#txtAge").show();
} else {
  $("#txtAge").hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
  Age is selected
</div>

How do I successfully query the checked property?

2
  • 6
    Starting jquery 1.6 there have been significant changes the way attributes and properties are dealt with. For your case following should work: if($('#isAgeSelected').prop("checked")) { $("#txtAge").show(); } else { $("#txtAge").hide(); } The condition in if statement will simply return true or false depending upon the checked/unchecked state of the check box. For more details refer to attributes vs. properties section on this link.
    – RBT
    Commented Feb 12, 2016 at 18:44
  • 3
    Does this answer your question? How can I check if a checkbox is checked?
    – Liam
    Commented May 13, 2022 at 13:51

68 Answers 68

1 2
3
3

In case you need to know if a checkbox is checked in pure javascript you should use this code .

let checkbox =document.getElementById('myCheckboxId');
if(checkbox.checked) {
    alert("element is checked");
} else {
    alert("element is  ot checked");
}
2
if( undefined == $('#isAgeSelected').attr('checked') ) {
    $("#txtAge").hide();
} else {
    $("#txtAge").show();
}
2

For older versions of jQuery, I had to use following,

$('#change_plan').live('click', function() {
     var checked = $('#change_plan').attr('checked');
     if(checked) {
          //Code       
     }
     else {
          //Code       
     }
});
1

In case if you need to use CSS class as jQuery selector you can do following:

$(document).ready(function () {
        $('.myOptionCheckbox').change(function () {            
            if ($(this).prop('checked') == true) {
                console.log("checked");           
            }
            else {
                console.log("unchecked");                
            }
        });
    });

It works fine for checkboxes and radioboxes as well.

1

This function is alternative and stable:

$('#isAgeSelected').context.checked
(return True/False)

Example:

if($('#isAgeSelected').context.checked){ //if Checkbox is checked then bla bla..
    /*.....*/
}else{
    /*.....*/
}
1
$('#chk').change(function() { 
    (this.checked)? alert('true') : alert('false');
});



($('#chk')[0].checked)? alert('true') : alert('false');
0
if($('#isAgeSelected').prop('checked')) {
    // do your action 
}
-1

$("#isAgeSelected").prop('checked', true);

1
  • 3
    This code can set the checkbox to checked status.I do not think this answer is relative to the question.
    – bronze man
    Commented Apr 3, 2015 at 0:51
1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.