HTML <tbody> data-* Attribute

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

Example

#

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

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; }
  .tb tbody { font-style: italic; }
</style>

<table class="tb">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
    </tr>
  </thead>

  <tbody data-title="List of active customers">
    <tr>
      <td>Denice</td>
      <td>Hobermann</td>
    </tr>
    <tr>
      <td>Paulo</td>
      <td>Cornell</td>
    </tr>
  </tbody>
</table>

Using data-*

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

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

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


Syntax

<tbody 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-content attribute on a <tbody> element.
Clicking the button will display the content value.

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; }
  .tb tbody { font-style: italic; }
</style>

<table class="tb">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
    </tr>
  </thead>

  <tbody id="mytbody" data-content="Active customers">
    <tr>
      <td>Denice</td>
      <td>Hobermann</td>
    </tr>
    <tr>
      <td>Paulo</td>
      <td>Cornell</td>
    </tr>
  </tbody>
</table>

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

<script>
  let toggle = () => {
    let element = document.getElementById("mytbody");
    alert("Content = " + element.getAttribute('data-content'));
  }
</script>

Code explanation

The <tbody> tag has a custom data-content attribute.

The data-content attribute specifies the content of the <tbody>.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the <tbody> content.

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 <tbody>
Guides