Null Pointer
Null Pointer

Reputation: 9289

Get number of checked checkboxes( having same class) -Jquery

In my MVC page i am generating a group of check boxes. Every check box have sameclass called 'resultTableCheckBox'

I want to get number of selected checkboxes .I tried following code

  $(".resultTableCheckBox").live('click', function (event) {
           alert($(".resultTableCheckBox :checked").length);
         });

But above code always alert 0. I tried

  $(".resultTableCheckBox").live('click', function (event) {
           alert($(".resultTableCheckBox ").length);
         });

Now its alert the total number of checkboxes having same class resultTableCheckBox. But how can i get number of selected check boxes

Upvotes: 2

Views: 982

Answers (3)

ahoka
ahoka

Reputation: 58

The :checked selected should not have a space. Tested it and it works.

$(".resultTableCheckBox:checked")

Upvotes: 1

Talljoe
Talljoe

Reputation: 14827

Your selector is finding all children of all .resultTableCheckBox elements that are checked. You need to add the ":checked" selector directly to the class.

$(".resultTableCheckBox:checked")

Upvotes: 5

Orbling
Orbling

Reputation: 20602

Lose the space:

$(".resultTableCheckBox:checked").length

Upvotes: 3

Related Questions