Get tr element id if checked

With the code below, I get the trelements attributes id:

var IDs = [];
$(".head").each(function(){ IDs.push(this.id); });
alert(IDs);

These items trhave checkboxes.

I want that if the checkboxes are checked, I have tags tr. I need to set trids checkboxes :)

How can i achieve this?

+3
source share
3 answers

You need this to get the parent id of the checked flags ...

    var IDs = [];
    $(".head input:checked").each(function(){ IDs.push($(this).parent().attr("id")); });
    alert(IDs);

Here is a working example ...

http://jsfiddle.net/uMfe3/

+2
source

You can do it like this ...

var Ids = $('.head:has(:checkbox:checked)')
           .map(function() { return this.id })
           .get();

If you want it to run faster using jQuery's internal use querySelectorAll(), you can use ...

var Ids = $('.head').filter(function() {
              return $(this).has('input[type="checkbox"]') && this.checked;
          });

..., jQuery .head, .

+1

-

var IDs = [];
//iterate over your <tr>
$(".head").each(function(){ 
    //if there is atleas a checked checkbox
    if($('input:checkbox:checked', this).length > 0){ 
        //add the id of the <tr>
        IDs.push(this.id); 
    }
});

alert(IDs);

$(".head input:checkbox:checked").each(function(){ 
    IDs.push(this.id); 
});
0

All Articles