简体   繁体   中英

Read td values by tr id from the same table

I am trying to read the values of table based on the tr id, but cannot wrap my head around how to do this.

    // Id of the tr in question for example.row_17 
    var d =c["id"]; 
    console.log(d); 

    // Here I can read all the td values, but I only want
    // the ones inside the tr id = "row_17" 

    var cols =document.getElementById('report_table').getElementsByTagName('td'), 

     colslen = cols.length, i = -1; > while(++i < colslen)
    {  console.log(cols[i].innerHTML); 

}

Since you tagged it with jQuery, you can do it by doing something like this:

var id = c["id"];

// Select only the row with id specified in 'id' and loop through all 'td's' of that row.
$("#" + id).find("td").each(function()
{
    // Log the html content of each 'td'.
    console.log($(this).html());
});

Just in case you want a solution for JavaScript only (no jQuery):

var id = c["id"];

var tds = document.querySelectorAll('#' + id + ' td');
for(var i = 0; i < tds.length; i++) {
    console.log(tds[i].innerHTML);
}

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM