TheCarver
TheCarver

Reputation: 19713

Counting checkboxes in JS/JQuery

I have this simple line, that I was hoping would count all checked checkboxes with the class (.photos):

var count = $('.photos').length;

It counts all the checkboxes that have that class (.photos) but it doesn't count the checked ones.

Does anybody know how to alter this line to count the checked checkboxes within this class?

Regards

Upvotes: 0

Views: 93

Answers (2)

James Allardice
James Allardice

Reputation: 165961

You can use the :checked pseudo-selector:

var count = $(".photos:checked").length;

Upvotes: 9

SLaks
SLaks

Reputation: 887365

You're looking for the :checked selector, which will only match checked elements.

Upvotes: 2

Related Questions