How to count check-boxes using jQuery?

daGrevis picture daGrevis · Nov 4, 2011 · Viewed 132.2k times · Source

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!

Answer

Nicola Peluchetti picture Nicola Peluchetti · Nov 4, 2011

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;