Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting checked checkboxes

I'm having a problem counting checked checkboxes in jQuery that runs counter to every example I could find. The following code works:

var checked_boxes = $('input:checkbox:checked').length // Returns 1 or 2 etc.

However, I would like to modify that to count the number of checkboxes of a particular class, say my_class. I have tried the following:

var checked_boxes = $('input.my_class:checked').length // Always 0
var checked_boxes = $('input.my_class:checkbox:checked').length // Always 0
var checked_boxes = $('input[type=checkbox].my_class:checked').length // 0 also

Same with several other syntaxes/permutations I tried.

For some background, these checkboxes are in a table in a td and essentially look like so:

<input type="checkbox" id="cb1" class="some_class_for_display_style my_class" value="1" />Blah

Any idea what I'm missing here?

EDIT:

Found the problem: It was the mis-placed class. I added an answer to that effect.

like image 943
Adnan Avatar asked Mar 14 '12 02:03

Adnan


1 Answers

Remove the $ sign in the selector.

$('input.my_class:checked').length - Count of checkboxes which are checked

$('input.my_class').length - Count of all checkboxes with class my_class

like image 59
westo Avatar answered Dec 10 '22 16:12

westo