0

I have a table produced by a getJSON ajax call and I want to loop through and collect the checked values and put them into a database.

The table looks like this:

<div class="content">
<table>
<tr> <td> etc. </td> </tr>
</table>
</div>

The content div exists but the table, the rows and cells are created by the ajax script so:

$.each( data, function( key, value ) {
        var txt =  '<tr>';
            txt += '<td style=\"background:#E3F6CE;\"><input type=\"checkbox\" class=\"case\" value=\"' + this["id"] + '\">';
            txt += '<td><a href=\"/edit/' + this["id"] + '\">'+ this["first"] + ' ' + this["last"]  + '</a></td>';
            txt += '<td>' + this["designation"] + '</td>';
        txt += '<td>' + this["company"] + '</td>';
            txt += '<td>' + this["email"] + '</td>';
            txt += '<td>' + this["remarks"] + '</td>';
        txt += '</tr>';
            $('.content').append(txt);
        });

So far so good, no problems. The table renders fine.

Now, the following:

$(document).ready(function() {
 $(".content").on( "input:checked" ).each(function() {
     //var id = $(this).val();
        var id = $( "input:checked" ).val();
              alert(id);
  });
});

only grabs one id, so if multiple checkboxes are checked it only gets one, not all.

What's going wrong?

Thanks in advance.

user1903663
  • 1,713
  • 2
  • 22
  • 44

1 Answers1

1

You need to create an array for that try like this.

$(document).ready(function() {

    $(".add").on("click",function(){
        var chk_vals = [];
        $("input:checked").each(function() {
             var id = $(this).val();
             console.log(id); // use this to check each checkbox value in console
             chk_vals.push(id);  
        });
    console.log(chk_vals); // use this to check the result in console.
    });

});
Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35