Check if checkbox input is checked using jQuery
We used .attr() method to get the attribute value of a checkbox. Which would return "checked" if the checkbox state is checked. But as of jQuery 1.6 it will return undefined if checkbox state is not set.Therefore, if you are still using previous jQuery, you can check state of checkbox input like so :JQUERY
- 1
- 2
- 3
- 4
- 5
//traditional way, before jQuery 1.6
var is_checked = $('#mycheckbox').attr("checked");
if(is_checked == "checked"){
//do stuff
}
jQuery 1.6+
Those of you using jQuery 1.6+, you can use .prop() method instead to find-out checkbox state, which returns true or false.JQUERY
- 1
- 2
- 3
- 4
var is_checked = $('#mycheckbox').prop('checked');
if(is_checked){
//do something
}