HTML <col> data-* Attribute

The data-* attribute on a <col> tag attaches additional data to the col 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-category attribute on a <col> tag.
The attribute value is not visible, but is readable by JavaScript.

Id First Name Last Name Country
ART-303 Vincent Van Gogh Netherlands
ART-877 Paul Cézanne France
<style>
  table.tb { width:100%; border-collapse: collapse; }
  .tb th, .tb td { padding:3px; border: 1px solid #777; }
</style>

<table class="tb">
  <colgroup>
    <col data-category="ART">
    <col span="3" style="background-color:aliceblue;">
  </colgroup>
  <tr> 
      <th>Id</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Country</th>
   </tr>
   <tr> 
      <td>ART-303</td>
      <td>Vincent</td>
      <td>Van Gogh</td>
      <td>Netherlands</td>
   </tr>
   <tr> 
      <td>ART-877</td>
      <td>Paul</td>
      <td>Cézanne</td>
      <td>France</td>
   </tr>
</table>

Using data-*

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

The * part is replaced with a lowercase string, such as data-id, data-source, data-category, etc.

An <col> 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 or col.


Syntax

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

Id First Name Last Name Country
ART-303 Vincent Van Gogh Netherlands
ART-877 Paul Cézanne France

<style>
  table.tbl { width:100%; border-collapse: collapse; }
  .tbl th, .tbl td { padding:3px; border: 1px solid #777; }
</style>

<table class="tbl">
  <colgroup>
    <col id="mycol" data-category="ART">
    <col span="3" style="background-color:aliceblue;">
  </colgroup>
  <tr> 
      <th>Id</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Country</th>
   </tr>
   <tr> 
      <td>ART-303</td>
      <td>Vincent</td>
      <td>Van Gogh</td>
      <td>Netherlands</td>
   </tr>
   <tr> 
      <td>ART-877</td>
      <td>Paul</td>
      <td>Cézanne</td>
      <td>France</td>
   </tr>
</table>

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

<script>
  let show = () => {
    let element = document.getElementById("mycol");
    alert("Category = " + element.getAttribute('data-category'));
  }
</script>

Code explanation

The <col> tag contains a custom data-category attribute.

Button clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the col category.


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