Test if at least one checkbox is checked

Avatar of Chris Coyier
Chris Coyier on

In this example, a submit button is disabled if none of the checkboxes are checked and enabled if at least one is checked.

   <!-- bunch of checkboxes like: -->
   <input type="checkbox" ... >
   <input type="checkbox" ... >

   <!-- submit button, defaults to disabled -->
   <input type="submit" value="submit">

The trick is that you can use .is(":checked") on a jQuery object full of a bunch of elements and it’ll return true if any of them are checked and false if none of them are. AND, using .attr() for the disabled attribute with that boolean value will enable/disable that button.

var checkboxes = $("input[type='checkbox']"),
    submitButt = $("input[type='submit']");

checkboxes.click(function() {
    submitButt.attr("disabled", !checkboxes.is(":checked"));