mr.cool mr.cool - 3 months ago 6
jQuery Question

how to get the id of a tr inside the loop

hi i got the tr content like this way simil



var td = $("tr td"); // get first child of all the td elements
var htmlContent = []; // initilize an empty array
for (i = 0; i < td.length; i++) {
htmlContent[i] = $(td[i]).text();
trid[i] = $(td[i]).attr("id");

}





i want the tr id so i use this code


trid[i] = $(td[i]).attr("id");


but this is not good

Answer

You can use .parent() to get the tr, and then its id.

var htmlContent = []; // initilize an empty array
$('tr td').each(function () {
    htmlContent.push($(this).text());
     console.log($(this).parent().attr('id'));
});

Here is a simple fiddle: https://jsfiddle.net/hxsbLws2/1/

Comments