jQuery check if any checkboxes are checked

JavaScript performance comparison

Revision 2 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 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) {
      counter++;
    }
  }
}
var isOneChecked =counter;


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

1 comment

Punithavel commented :

Excellent ! i like this very much

Add a comment