Testing if a checkbox is checked with jQuery Testing if a checkbox is checked with jQuery javascript javascript

Testing if a checkbox is checked with jQuery


Use .is(':checked') to determine whether or not it's checked, and then set your value accordingly.

More information here.


$("#ans").attr('checked') 

will tell you if it's checked. You can also use a second parameter true/false to check/uncheck the checkbox.

$("#ans").attr('checked', true);

Per comment, use prop instead of attr when available. E.g:

$("#ans").prop('checked')


Just use $(selector).is(':checked')

It returns a boolean value.