Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count check-boxes using jQuery?

I have tons of checkboxes that are either checked (checked="checked") or unchecked.

I would like to get the number of all checkboxes, unchecked and checked checkboxes.

With check-box I mean <input type="checkbox" />.

How to do it with jQuery? Thanks in advance!

like image 477
daGrevis Avatar asked Nov 04 '11 15:11

daGrevis


People also ask

How do I count the number of checkboxes checked?

Counting Checkboxes So how do you know how many checkboxes you've checked? Type an equals sign into a cell. Use the =COUNTIF function.

How check if checkbox is checked jQuery?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes.


2 Answers

You could do:

var numberOfChecked = $('input:checkbox:checked').length; var totalCheckboxes = $('input:checkbox').length; var numberNotChecked = totalCheckboxes - numberOfChecked; 

EDIT

Or even simple

var numberNotChecked = $('input:checkbox:not(":checked")').length; 
like image 125
Nicola Peluchetti Avatar answered Oct 15 '22 21:10

Nicola Peluchetti


Assume that you have a tr row with multiple checkboxes in it, and you want to count only if the first checkbox is checked.

You can do that by giving a class to the first checkbox

For example class='mycxk' and you can count that using the filter, like this

$('.mycxk').filter(':checked').length 
like image 34
Samuel Aiala Ferreira Avatar answered Oct 15 '22 21:10

Samuel Aiala Ferreira