Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use jQuery to loop through an unspecified number of checkboxes?

I need to loop through a div full of an unknown number of checkboxes and get back the values of the checkboxes that are checked, how do I do this with jQuery? I guess it should happen onchange of any of the checkboxes.

like image 850
MetaGuru Avatar asked Dec 08 '22 06:12

MetaGuru


2 Answers

Just do:

<div id="blah"> 
  <input type="checkbox" class="mycheckbox" />
  <input type="checkbox" class="mycheckbox" />
  <input type="checkbox" class="mycheckbox" />
</div>

$("#blah .mycheckbox").each(function(){
  alert(this + "is a checkbox!");
});
like image 160
marcgg Avatar answered Dec 09 '22 20:12

marcgg


Try:

<div id="blah"> 
  <input type="checkbox" class="mycheckbox" />
  <input type="checkbox" class="mycheckbox" />
  <input type="checkbox" class="mycheckbox" />
</div>

$("#blah .mycheckbox:checked").each(function(){
  alert($(this).attr("value"));
});
like image 38
Ender Avatar answered Dec 09 '22 20:12

Ender