How to get row id from row Index in table using JavaScript

html, javascript

Solution

Assuming you have only one table on your page:

document.getElementsByTagName("tr")[index].id;

Preferably though, you'd give your `table` a `id`, though, and get your row like this:

<table id="tableId">
    <tr id="a">
        <td>a</td>
    </tr>
    <tr id="b">
        <td>b</td>
    </tr>
</table>
var table = document.getElementById("tableId");
var row = table.rows[index];
console.log(row.id);

This way, you can be certain you don't get any interference, if you have multiple tables in your page.

Problem

Suppose this is my table: ``` <table> <tr id="a"> <TD>a</TD> </tr> <tr id="b"> <TD>b</TD> </tr> </table> ``` How can I get row id using the row index from a table? Above is just an example where id is static but in my case my id is dynamic, so I can't use `document.getElementById()`.

Original source