Determining if a checkbox is checked with jQuery
Posted by andy gaskell on Sep 14th, 2009
This one comes up quite often on the Stack Overflow, so I’ll give my shot at answering.
If you have a single checkbox that you’d like to check I like this style:
if($('#your_checkbox_id').is(':checked')) {
alert('checked!');
} else {
alert('not checked!');
}
Here’s another way of determining of a checkbox is checked:
if($('#your_checkbox_id:checked').length) {
alert('checked!');
} else {
alert('not checked!');
}
Here’s more jQuery checkbox selector goodness.
This selector will get all of the checkboxes (checked or unchecked) in the DOM:
$(':checkbox');
All of the checked checkboxes:
$(':checkbox:checked');
All of the unchecked checkboxes:
$(':checkbox').not(':checked');
Here’s all of the checkboxes within a form:
$('form :checkbox');
How to check all checkboxes:
$(':checkbox').attr('checked', 'checked');
Unchecking all checkboxes:
$(':checkbox').removeAttr('checked');
September 15th, 2010 at 9:59 am
This is the clearest jQuery checkbox reference I’ve come across… any chance you plan on picking up this blog again (or rewriting the official jQuery docs?)
March 26th, 2012 at 8:02 pm
[...] more awesome jQuery checkbox reference material: http://gaskell.org/determining-if-a-checkbox-is-checked-with-jquery/ Share and [...]