Snote
Snote

Reputation: 955

Checked checkbox in jquery

I have a varible IDs with 3 checkboxes ids (#Id1, #Id2, #Id3).
So when I do $(IDs), I have the list of the checkboxes.

How to know how many checkboxes are checked? I want to trigger the click event on the checked checkboxes.

I try $(IDs + ":checked").click() but the :checked is only on #Id3.

I could do $(IDs).each(...); and do in the function the test if it's checked.
Or change the value of IDs to this : #Id1:checked, #Id2:checked, #Id3:checked.

Is there a way to do it in one line? (or easily)

Thanks

Upvotes: 2

Views: 216

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 191036

Use filter.

var $checked = $(IDs).filter(':checked');
$checked.click(function() { alert('hello'); });

Upvotes: 5

Related Questions