Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

HTML <table> data-* Attribute

A data-* attribute on a <table> tag attaches additional data to the table.

To create a custom attribute, replace * with a lowercase string, such as data-id, data-status, or data-location.

Example

#

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

First name Last name
Denice Hobermann
Paulo Cornell
Jane Hollander
<style>
  table.tb { border-collapse: collapse; width:300px; }
  .tb th, .tb td { padding: 5px; border: solid 1px #777; }
  .tb th { background-color: lightblue;}
</style>

<table data-title="Customer List" class="tb">
  <tr>
    <th>First name</th>
    <th>Last name</th>
  </tr>
  <tr>
    <td>Denice</td>
    <td>Hobermann</td>
  </tr>
  <tr>
    <td>Paulo</td>
    <td>Cornell</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>Hollander</td>
  </tr>
</table>

Using data-*

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

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

An <table> 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 table.


Syntax

<table 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-records attribute on a <table>.
Clicking the button will display the records value.

First name Last name
Denice Hobermann
Paulo Cornell
Jane Hollander

<style>
  table.tb { border-collapse: collapse; width:300px; }
  .tb th, .tb td { padding: 5px; border: solid 1px #777; }
  .tb th { background-color: lightblue;}
</style>

<table id="mytable" data-record-type="Customers" class="tb">
  <tr>
    <th>First name</th>
    <th>Last name</th>
  </tr>
  <tr>
    <td>Denice</td>
    <td>Hobermann</td>
  </tr>
  <tr>
    <td>Paulo</td>
    <td>Cornell</td>
  </tr>
  <tr>
    <td>Jane</td>
    <td>Hollander</td>
  </tr>
</table>

<br/>
<button onclick="show();">Show data</button>

<script>
  let show = () => {
    let element = document.getElementById("mytable");
    alert("Record type = " + element.getAttribute('data-records'));
  }
</script>

Code explanation

The <table> tag has a custom data-records attribute.

The data-records attribute stores the records of the information inside the <table>.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the records of the <table>.

Note: Notice how the title 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 <table>
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

Guides