mercredi 23 septembre 2015

Check All the checkboxes in a table when a particular checkbox is clicked?

I am dynamically adding checkboxes using tr and td to a table this way.

var arr=[1,2,3];

                var trtest = "<tr>";
                trtest = trtest +"<tr><td><input type='checkbox' onclick=onAllCheckBoxClick('" + arr+ "') class = 'all' id ='all'>All</td></tr>";

$.each(arr, function(i, tmp) {
                    trtest = trtest +"<tr><td><input type='checkbox' onclick=onCheckBoxClick('" + this+ "','" + arr+ "') class = 'all' id ='"+tmp+"'/>"+tmp+"</td></tr>";
                });

This code is working And i am getting 4 checkboxes this way,

All
    1

    2

    3

Here, when i try to check All checkbox then all the other checboxes in the same table 1,2,3 have to be selected.

I tried adding this click event to All checkbox.

function onAllCheckBoxClick(arr){
        var checked = $('#all').attr('checked');
        if(checked =="checked") {
            $.each(arr.split(","), function(i, tmp) {
                    $('#'+tmp).attr('checked',true);
            }); 
        }

}

Issue is when i check All it is only checking 3(i.e, last checkbox) in the same table, where 1,2 remained un-checked.

But I want all 1,2,3 checkboxes to be checked when i check All checkbox.

can anyone help me in this issue?




Aucun commentaire:

Enregistrer un commentaire