The data-*
global attributes form a class of attributes called custom data attributes, that allow proprietary information to be exchanged between the HTML and its DOM representation by scripts.
All such custom data are available via the HTMLElement
interface of the element the attribute is set on. The HTMLElement.dataset
property gives access to them.
The *
may be replaced by any name following the production rule of XML names with the following restrictions:
- the name must not start with
xml
, whatever case is used for these letters; - the name must not contain any semicolon (
U+003A
); - the name must not contain capital letters.
Note that the HTMLElement.dataset
property is a DOMStringMap
, and the name of the custom data attribute data-test-value will be accessible via HTMLElement.dataset.testValue
(or by HTMLElement.dataset["testValue"]
) as any dash (U+002D
) is replaced by the capitalization of the next letter, converting the name to camelcase.
Usage
By adding data-*
attributes, even ordinary HTML elements can become rather complex and powerful program-objects. For example, a space-ship "sprite" in a game could be a simple <img>
element with a class
attribute and several data-*
attributes:
<img class="spaceship cruiserX3" src="shipX3.png" data-ship-id="324" data-weapons="laserI laserII" data-shields="72%" data-x="414354" data-y="85160" data-z="31940" onclick="spaceships[this.dataset.shipId].blasted()">
For a more in-depth tutorial about using HTML data attributes, see Using data attributes.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'data-*' in that specification. |
Living Standard | No change from latest snapshot, HTML 5.1 |
HTML 5.1 The definition of 'data-*' in that specification. |
Recommendation | Snapshot of HTML Living Standard, no change from HTML5 |
HTML5 The definition of 'data-*' in that specification. |
Recommendation | Snapshot of HTML Living Standard, initial definition. |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
data-* attributes | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
See also
- All global attributes.
- The
HTMLElement.dataset
property that allows to access and modify these values. - Using data attributes