tags:

views:

69

answers:

3

How do I find out if the users have checked any of the boxes with jQuery? Thanks.

<div id="boxes">
<input id='id1' type='checkbox'>
<input id='id2' type='checkbox'>
<input id='id3' type='checkbox'>
</div>
A: 
var checked = $("input[type=checkbox]:checked");
Paul Tarjan
A: 

The checked selector will select all checkboxes.

// Selector for all checked checkboxes.
var checkboxes = $("input:checked");

If you just want to know if any of them are checked, or how many, check the length of the selector. If it's > 0, at least one checkbox is selected. However, if you only want one box checked at a time, consider using a radio button instead (which also uses the checked selector).

var count = $("input:checked").length;
R. Bemrose
A: 
var anychecked = 0 < $('#boxes :checkbox:checked').length();
Scott Evernden