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');

2 Responses

  1. soychicka Says:

    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?)

  2. jQuery .is method. | web2.0goodies.com Says:

    [...] more awesome jQuery checkbox reference material: http://gaskell.org/determining-if-a-checkbox-is-checked-with-jquery/ Share and [...]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.