This post will discuss how to count the total number of checked checkboxes in JavaScript and jQuery.

1. Using jQuery

With jQuery, you can use the $('input[type="checkbox"]'), which returns the list of all checkboxes.

JS


HTML



Edit in JSFiddle

 
It can be shortened to $('input:checkbox')

JS


HTML



Edit in JSFiddle

 
To get a list of all checked checkboxes, you can use the :checked selector like $('input:checkbox:checked').

JS


HTML



Edit in JSFiddle

 
Alternatively, to get all unchecked checkboxes, you can use the :not() pseudo-class, which selects any elements that don’t match the specified selectors.

JS


HTML



Edit in JSFiddle

2. Using JavaScript

With pure JavaScript, you can use the Document method querySelectorAll(), which returns a list of the document’s elements that match the specified group of selectors.

JS


HTML



Edit in JSFiddle

That’s all about counting the number of checkboxes in JavaScript and jQuery.