Dofactory.com
Dofactory.com
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 freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <section> data-* Attribute

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

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 <section>.
The attribute value is not visible, but it is readable by JavaScript.

The Night Watch

The Night Watch (Dutch: De Nachtwacht), is a 1642 painting by Rembrandt van Rijn. It is in the collection of the Amsterdam Museum but is prominently displayed in the Rijksmuseum (State Museum) as the best-known painting in its collection. The Night Watch is the most famous Dutch Golden Age painting.

<section data-title="The Night Watch">
  <h2>The Night Watch</h2>
  <p>The Night Watch (Dutch: De Nachtwacht), is a 1642 
     painting by Rembrandt van Rijn. It is in the collection 
     of the Amsterdam Museum but is prominently displayed 
     in the Rijksmuseum (State Museum) as the best-known 
     painting in its collection. The Night Watch is 
     the most famous Dutch Golden Age painting.
  </p>
</section>

Using data-*

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

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

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


Syntax

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

The Night Watch

The Night Watch (Dutch: De Nachtwacht), is a 1642 painting by Rembrandt van Rijn. It is in the collection of the Amsterdam Museum but is prominently displayed in the Rijksmuseum (State Museum) as the best-known painting in its collection. The Night Watch is the most famous Dutch Golden Age painting.


<section id="mysection" data-topic="The Night Watch">
  <h2>The Night Watch</h2>
  <p>The Night Watch (Dutch: De Nachtwacht), is a 1642 
     painting by Rembrandt van Rijn. It is in the collection 
     of the Amsterdam Museum but is prominently displayed 
     in the Rijksmuseum (State Museum) as the best-known 
     painting in its collection. The Night Watch is 
     the most famous Dutch Golden Age painting.
  </p>
</section>

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

<script>
  let show = () => {
     let element = document.getElementById("mysection");
     alert("Topic = " + element.getAttribute('data-topic'));
  }
</script>

Code explanation

The <section> tag has a custom data-topic attribute.

The data-topic attribute holds the topic of the <section> content.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays <section> topic.

Note: Notice how the title displays immediately without server call.


Browser support

Here is when data-* support started for each browser:

Chrome
6.0 Sep 2010
Firefox
4.0 Mar 2011
IE/Edge
9.0 Mar 2011
Opera
11.1 Mar 2011
Safari
5.0 Jun 2010

You may also like

 Back to <section>

Last updated on Sep 30, 2023

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 freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides