Skip to content Skip to sidebar Skip to footer

Finding And Counting Number Of Checked Boxes Using JQuery

I have one form on a page and within that form there will be a check box for every row in the table. I need to count the number of rows that have a checked row, but I am having tro

Solution 1:

var q = $('form#id-of-form').find('input:checked').length;

http://api.jquery.com/checked-selector/

If there are checkboxes in the form you don't want counted, then add a class to the ones you do want counted and select those instead:

var q = $('form#id-of-form').find('input.count-these:checked').length;

Solution 2:

jQuery provides special selectors for this:

$('input:checked').length

More specifically:

var num = $('#myform input:checkbox:checked').length;

I added the jQuery :checkbox selector because :checked applies to radio buttons as well. Technically, :checkbox is slower than [type=checkbox] in modern browsers, but they do the same thing:

var num = $('#myform input[type=checkbox]:checked').length;

Solution 3:

You could try this:

$('#form input:checkbox:checked').length;

Solution 4:

$('#myForm table tr td input:checked')​.size();​

DEMO.

Update: Better to filter type because you could have checked radio buttons too

$('#myForm table tr td input[type="checkbox"]:checked').size();

Demo with checked radio buttons


Post a Comment for "Finding And Counting Number Of Checked Boxes Using JQuery"