2

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

Reporter
  • 3,897
  • 5
  • 33
  • 47
Snote
  • 955
  • 3
  • 19
  • 36
  • put a class in each one, make a click event in the class, get the ID and try something `if($(id).is(":checked")){ }` ... – Ricardo Binns Feb 07 '12 at 13:32
  • You can see how to do what you want at this answer: http://stackoverflow.com/a/2204275/132528 – JasCav Feb 07 '12 at 13:32

1 Answers1

5

Use filter.

var $checked = $(IDs).filter(':checked');
$checked.click(function() { alert('hello'); });
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445