jQuery check if any checkboxes are checked

JavaScript performance comparison

Revision 5 of this test case created

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

0 comments

Add a comment