jQuery check if any checkboxes are checked

JavaScript performance comparison

Test case created by Richard

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="checkboxes">
</div>
<script>
  $('#checkboxes').html(
            new Array(400).join('<input type="checkbox" value="1" />')
          + new Array(400).join('<input type="checkbox" value="1" checked />')
  );
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Using $.fn.is()
var isOneChecked = $("input[type='checkbox']").is(":checked");
pending…
Using :checked
var isOneChecked = $("input[type='checkbox']:checked").length > 0;
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

2 comments

Nick Davis commented :

I have a page in the works that has multiple groups of checkboxes. On SUBMIT, I need to be able to check each group separately for one or more being checked. Is there a way to limit the scope of the check to those with the same class?

Dani commented :

I didn't know that there would be such a performance hit.

Just goes to show that being elegant, i.e. using is(":checked"), isn't always the best technique!

Add a comment