Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I check whether a checkbox is checked in jQuery?

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="(ajaxgoogleapilink)"></script>
<input type="checkbox" id="isAgeSelected"/>
<div id="txtAge" style="display:none">
Age is selected
</div>

How do I successfully query the checked property?
by

3 Answers

kshitijrana14
Use jQuery's is() function:
if($("#isAgeSelected").is(':checked'))
$("#txtAge").show(); // checked
else
$("#txtAge").hide(); // unchecked
MounikaDasa
This worked for me:

$get("isAgeSelected ").checked == true

Where isAgeSelected is the id of the control.
sandhya6gczb

var active = $('#modal-check-visible').prop("checked") ? 1 : 0 ;

If checked, it'll return 1; otherwise, it'll return 0

Login / Signup to Answer the Question.