jQuery checkboxes

May 15, 2012 by Jean

Very useful reference at http://technosophos.com/node/223 on how to properly manipulate checkboxes with jQuery.

Cured my headache better than Advil.

Check if a checkbox is checked

$('#my-checkbox').is(':checked');

Check a checkbox

Following the HTML spec, the attribute must be set to checked="checked" (not just checked).

$('#my-checkbox').attr('checked','checked');

Uncheck a checkbox

$('#my-checkbox').removeAttr('checked');

Check / Uncheck all checkboxes

$(':checkbox:not(:checked)').attr('checked', 'checked');
$(':checkbox:checked').removeAttr('checked');

In: Javascript