jQuery check if any checkboxes are checked

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
<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 formobj = document.forms[0];
var counter = 0;
for (var j = 0; j < formobj.elements.length; j++) {
  if (formobj.elements[j].type == "checkbox") {
    if (formobj.elements[j].checked) {
var isOneChecked =counter;

Using :checked
var isOneChecked = $("input[type='checkbox']:checked").length;

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:

1 comment

Punithavel commented :

Excellent ! i like this very much

Comment form temporarily disabled.

Add a comment