HTML5 Custom Data Attribute

By Xah Lee. Date: . Last updated: .

HTML5 has a “custom data attribute”. It lets you have any attribute/value pairs in a tag. Such attribute's name starts with data-. Here's a example used by Twitter:

<a href="https://twitter.com/share" class="twitter-share-button" data-via="john">Tweet</a>

here's what the html standard say:

A custom data attribute is an attribute in no namespace whose name starts with the string "data-", has at least one character after the hyphen, is XML-compatible, and contains no ASCII upper alphas.

[2020-04-20 from https://html.spec.whatwg.org/multipage/dom.html#custom-data-attribute]

Why's This Useful?

Here is one example of using it to store accessed date of a site.

<a href=url data-accessed="2021-08-07">some thing</a>

Using JavaScript to Access Custom Data Attribute

You can use JavaScript/DOM's “getAttribute” method, like any other attribute.

[see JS: Get Element's Attribute Value]