Sunday, June 9, 2013

How to Check a checkbox using JQuery

You can check easily if checkbox checked or not with jQuery 1.6+ with .prop() function:

// check if my checkbox is checked
$('.myCheckbox').prop('checked', true);

// check if my checkbox not checked
$('.myCheckbox').prop('checked', false);

For jQuery 1.5 and below the .prop() function is not available, so you need to use .attr().
To check the checkbox (by setting the value of the checked attribute) do


and for un-checking (by removing the attribute entirely) do

//Work on all version of jQuery

If you're working with just one element, it will always be fastest to use DOMElement.checked = true. The benefit to using the .prop() and .attr() functions is that they will operate on all matched elements.

// Assuming an event handler on a checkbox
if (this.checked)

Disqus Comments