Code Snippet
Test if at least one checkbox is checked
In this example, a submit button is disabled if none of the checkboxes are checked and enabled if at least one is checked.
<form>
<!-- bunch of checkboxes like: -->
<input type="checkbox" ... >
<input type="checkbox" ... >
<!-- submit button, defaults to disabled -->
<input type="submit" value="submit">
</form>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"));
});
That’s net!
elegant solution
so nice
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.