SpringLearner
SpringLearner

Reputation: 13854

How to know which tables's checkbox is selected?

This is a function

function collect_users_and_groups() {
   var tos = [];
   $('#mytable12 input:checked, #groupsTable1 input:checked').each(function(i, elt) {
       //alert("to groups");
       var dataids = $(this).parent().attr("data-selected").split(",");
       alert("dataids  "+dataids);
       var name = $.trim($(this).parent().next().text());
       tos.push(name);
   });

   return tos.join(', ');
}

which is called when I select check boxes Actually groupsTable1 has data-selected attribute but mytable12 does not have. I want to call this var dataids = $(this).parent().attr("data-selected").split(",");

when checkbox of groupsTable1 is clicked Please tell me how to do?

This is the full fiddle,the above js codes can be found in between 22-33 in the js section

Upvotes: 0

Views: 107

Answers (1)

Satpal
Satpal

Reputation: 133423

You can use .is() to check table is groupsTable1.

function collect_users_and_groups() {
   var tos = [];
   $('#mytable12 input:checked, #groupsTable1 input:checked').each(function(i, elt) {

        //Check table is groupsTable1
        if($(this).closest('table').is("#groupsTable1")){
            //alert("to groups");
            var dataids = $(this).parent().attr("data-selected").split(",");
            alert("dataids  "+dataids);
            var name = $.trim($(this).parent().next().text());
            tos.push(name);
        }
   });
   return tos.join(', ');
}

OR

Simply use

 $('#mytable12 input:checked, #groupsTable1 input:checked').each(function(i, elt) {

instead of

 $('#groupsTable1 input:checked').each(function(i, elt) {

Upvotes: 1

Related Questions