Hussy
Hussy

Reputation: 2029

Using the Id of td how to fing id of corresponding tr?

i have id of td and i want to get the id of parent tr? i have following code

 $(".isActive").click(function() {
   var checkBox_id = $(this).attr("id");          // id of checkbox inside td//
   var $this = $('#' + checkBox);
   var column_id = $this.parent().attr('id');     // id of td//
   var row_id =   ???                              // get id or tr using above td
 });

Upvotes: 1

Views: 168

Answers (5)

RoToRa
RoToRa

Reputation: 38441

i think you are missing a point here:

var checkBox_id = $(this).attr("id");
var $this = $('#' + checkBox);

The second line is is completly unnecessary. $this will refer to the same element as $(this). You just can do:

var $this = $(this);

for the same effect.

If you need the id of the tdand tr for the same thing, then you don't need it either.

var $td = $this.parent(); // Reference to td
var $tr = $td.parent(); // Reference to tr

Upvotes: 0

ajm
ajm

Reputation: 20105

Couple ways of doing it:

var row_id = $this.parent().parent().attr('id');
var row_id = $('#' + column_id).parent().attr('id'); 

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

You could use this i think

var row_id =   $this.closest('tr').attr('id')

Upvotes: 0

Alnitak
Alnitak

Reputation: 340055

There's no need to follow a chain of IDs - jQuery can find the closest enclosing table row for you:

$(".isActive").click(function() {
   var row_id = $(this).closest('tr').attr('id');
});

Upvotes: 4

Senad Meškin
Senad Meškin

Reputation: 13756

var column_id = $this.parent().parent().attr('id') //id of tr

Upvotes: 0

Related Questions