An id on a <tr> tag assigns an identifier to the table row.
The identifier must be unique across the page.
An id attribute on 2 <tr> elements.
First name | Last name |
---|---|
Denice | Hobermann |
Paulo | Cornell |
<style>
table.tb { width:300px; border-collapse: collapse; }
.tb th, .tb td { border: solid 1px #777; padding: 5px; }
.tb thead { background-color:lightblue; }
</style>
<table class="tb">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr id="userid-209">
<td>Denice</td>
<td>Hobermann</td>
</tr>
<tr id="userid-230">
<td>Paulo</td>
<td>Cornell</td>
</tr>
</tbody>
</table>
The id attribute assigns an identifier to the <tr> element.
The id allows JavaScript to easily access the <tr> element.
It is also used to point to a specific id selector in a style sheet.
Tip: id is a global attribute that can be applied to any HTML element.
<tr id="identifier" />
Value | Description |
---|---|
identifier | A unique alphanumeric string. The id value must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (-), underscores (_), colons (:), and periods (.). |
A <tr> element with a unique id.
Clicking the button displays the number of cells inside the first table row (i.e. header row).
First name | Last name |
---|---|
Denice | Hobermann |
Paulo | Cornell |
<style>
table.tb { width:300px; border-collapse: collapse; }
.tb th, .tb td { border: solid 1px #777; padding: 5px; }
.tb thead { background-color:lightblue; }
</style>
<table class="tb">
<thead>
<tr id="mytr">
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Denice</td>
<td>Hobermann</td>
</tr>
<tr>
<td>Paulo</td>
<td>Cornell</td>
</tr>
</tbody>
</table>
<br />
<button onclick="show();">Show # cells in a row</button>
<script>
let show = () => {
let element = document.getElementById("mytr");
let length = element.getElementsByTagName("th").length;
alert("# Cells = " + length);
}
</script>
The id attribute assigns a unique identifier for the <tr>.
Clicking the button calls JavaScript which locates the <tr> using the id.
Finally, the the number of <td> (table cells) inside the <tr> is displayed in an alert box.
Here is when id support started for each browser:
Chrome
|
1.0 | Sep 2008 |
Firefox
|
1.0 | Sep 2002 |
IE/Edge
|
1.0 | Aug 1995 |
Opera
|
1.0 | Jan 2006 |
Safari
|
1.0 | Jan 2003 |
Back to <tr>