A data-* attribute on a <tr> tag attaches additional data to the table row.
To create a custom attribute, replace * with a lowercase string, such as data-id, data-status, or data-location.
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>
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.
<tr data-*="value">
Note: The * can be any string, such as data-id, data-cost, data-supplier, etc.
| Value | Description |
|---|---|
| value | A string value. Can be numeric, alphanumeric, JSON, etc. |
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>
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.
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 |
Back to <tr>