jQuery check if any checkboxes are checked

JavaScript performance comparison

Test case created by Richard

Preparation code

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

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");
Using :checked
var isOneChecked = $("input[type='checkbox']:checked").length > 0;

Compare results of other browsers


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:


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!

Comment form temporarily disabled.

Add a comment