HTML <tr> id Attribute

The id attribute on a <tr> tag assigns an identifier to the table row element. The identifier must be unique across the page.

Example

#

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>

Using id

The id attribute assigns an identifier to the <tr> element.

The identifier must be unique across the page.

The id allows programmatic access to the <tr> element.

Tip:  id is a global attribute that can be applied to any HTML element.


Syntax

<tr id="identifier" />

Values

#

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 (.).

More Examples

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>

Code explanation

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.


Browser support

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

You may also like

 Back to <tr>
Guides