HTML <tr> data-* Attribute

The data-* attribute on a <tr> tag attaches additional data to the table row. To create a custom attribute, replace the * with a lowercase string, such as data-id, data-status, or data-location.

Example

#

A custom data-id attribute on a <tr> tag.
The attribute value is not visible, but is readable by JavaScript.

Customer 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>Customer name</th>
    </tr>
  </thead>

  <tbody>
    <tr data-id="1001">
      <td>Denice Hobermann</td>
    </tr>
    <tr data-id="1252">
      <td>Paulo Cornell</td>
    </tr>
  </tbody>
</table>

Using data-*

The data-* attribute adds custom information to a <tr> element.

The * part is replaced with a lowercase string, such as data-id, data-cost, or data-location.

An <tr> element can have any number of data-* attributes, each with their own name.

Using data-* attributes reduces the need for requests to the server.

Note: The data-* attribute is not visible and does not change the appearance of the tr.


Syntax

<tr data-*="value">

Note: The * can be any string, such as data-iddata-costdata-supplier,  etc.

Values

#

Value Description
value A string value. Can be numeric, alphanumeric, JSON, etc.

More Examples

A custom data-id attribute on <tr> tags.
Clicking a row will display the customer id value.

Customers
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">
  <tr>
    <th>Customers</th>
  </tr>
  <tr data-id="1001" onclick="show(this);">
    <td>Denice Hobermann</td>
  </tr>
  <tr data-id="1252" onclick="show(this);">
    <td>Paulo Cornell</td>
  </tr>
</table>

<script>
  let show = row => {
    alert("Customer Id: " + row.getAttribute('data-id'));
  }
</script>

Code explanation

The <tr> tag has a custom data-id attribute.

The data-id attribute stores the customer id.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the customer id.

Note: Notice how the customer id displays immediately without server call.


Browser support

Here is when data-* 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