0

I'm creating the checkboxes and corresponding ids for them dynamically as shown, according to the values in which I'm getting from back end.
Once it has been created, how could I retrieve the values of this checked checkboxes?

HTML:

<tr>
   <td class="valueleft">All</td>
   <td class="valueleft"><input type='checkbox' id="cb1"/></td>
</tr>
<tr>
   <td class="valueleft">--------</td>
   <td class="valueleft">----checkbox-------</td>
</tr>

jQuery:

$("#myTable").last().append("<tr><td>"+name+"</td><td><input type='checkbox'id="+id+"/></td></tr>");
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Suga
  • 57
  • 1
  • 1
  • 10
  • 2
    http://stackoverflow.com/questions/786142/how-to-retrieve-checkboxes-values-in-jquery – Binil Jul 19 '11 at 07:01

2 Answers2

3

To retrieve the values of the checked checkobxes you could do something like:

var checkedValues = [];

$('input[type=checkbox]:checked').each(function(){
       //here this refers to the checkbox you are iterating on
       checkedValues.push($(this).val());
});

or if you want name/values pair you could do:

var checkedValues = {};

$('input[type=checkbox]:checked').each(function(){
       //here this refers to the checkbox you are iterating on
       checkedValues[$(this).attr('id')] = $(this).val();
});

//you end up with an object with the id's as properties and the relative values as values
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

You could also use .map:

var checkedVals = $('input:checkbox:checked').map(function(){
   return $(this).val();
}).get();
Tryster
  • 199
  • 2