$('.myCheckbox').prop('checked', true); $('.myCheckbox').prop('checked', false);
If you’re working with just one element, you can always just access the underlying
HTMLInputElement and modify its
$('.myCheckbox').checked = true; $('.myCheckbox').checked = false;
The benefit to using the
.attr() methods instead of this is that they will operate on all matched elements.
jQuery 1.5.x and below
.prop() method is not available, so you need to use
$('.myCheckbox').attr('checked', true); $('.myCheckbox').attr('checked', false);
Note that this is the approach used by jQuery’s unit tests prior to version 1.6 and is preferable to using
$('.myCheckbox').removeAttr('checked'); since the latter will, if the box was initially checked, change the behaviour of a call to
.reset() on any form that contains it – a subtle but probably unwelcome behaviour change.
For more context, some incomplete discussion of the changes to the handling of the
checked attribute/property in the transition from 1.5.x to 1.6 can be found in the version 1.6 release notes and the Attributes vs. Properties section of the