The dataset read-only property of the HTMLOrForeignElement interface provides read/write access to custom data attributes (data-*) on elements. It exposes a map of strings (DOMStringMap) with an entry for each data-* attribute.
The dataset property itself can be read, but not directly written. Instead, all writes must be to the individual properties within the dataset, which in turn represent the data attributes.
An HTML data-* attribute and its corresponding DOM ' dataset.property modify their shared name according to where they are read or written:
- In HTML
- The attribute name begins with
data-. It can contain only letters, numbers, dashes (-), periods (.), colons (:), and underscores (_). Any ASCII capital letters (AtoZ) are ignored and converted to lowercase. - In JavaScript
- The property name of a custom data attribute is the same as the HTML attribute without the
data-prefix, and removes single dashes (-) for when to capitalize the property’s “camelCased” name.
In addition to the information below, you'll find a how-to guide for using HTML data attributes in our article Using data attributes.
Name conversion
dash-styletocamelCaseconversion- A custom data attribute name is transformed to a key for the
DOMStringMapentry by the following:
- Remove the prefix
data-(including the dash); - For any dash (
U+002D) followed by an ASCII lowercase letteratoz, remove the dash and uppercase the letter; - Other characters (including other dashes) are left unchanged.
- Remove the prefix
camelCasetodash-styleconversion- The opposite transformation, which maps a key to an attribute name, uses the following:
- Restriction: Before transformation, a dash must not be immediately followed by an ASCII lowercase letter
atoz; - Add the
data-prefix; - Add a dash before any ASCII uppercase letter
AtoZ, then lowercase the letter; - Other characters are left unchanged.
- Restriction: Before transformation, a dash must not be immediately followed by an ASCII lowercase letter
For example, a data-abc-def attribute corresponds to dataset.abcDef.
Accessing values
- Attributes can be set and read by the camelCase name/key as an object property of the dataset:
element.dataset.keyname - Attributes can also be set and read using bracket syntax:
element.dataset['keyname'] - The
inoperator can check if a given attribute exists:'keyname' in element.dataset
Setting values
- When the attribute is set, its value is always converted to a string.
For example:
element.dataset.example = nullis converted intodata-example="null". - To remove an attribute, you can use the
deleteoperator:delete element.dataset.keyname
Syntax
const dataAttrMap = element.dataset
Value
A DOMStringMap.
Examples
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe</div>
const el = document.querySelector('#user');
// el.id === 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
// set a data attribute
el.dataset.dateOfBirth = '1960-10-03';
// Result: el.dataset.dateOfBirth === '1960-10-03'
delete el.dataset.dateOfBirth;
// Result: el.dataset.dateOfBirth === undefined
if ('someDataAttr' in el.dataset === false) {
el.dataset.someDataAttr = 'mydata';
// Result: 'someDataAttr' in el.dataset === true
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| HTML Living StandardThe definition of 'HTMLElement.dataset' in that specification. | Living Standard | No change from latest snapshot, HTML 5.1 |
| HTML 5.1The definition of 'HTMLElement.dataset' in that specification. | Recommendation | Snapshot of HTML Living Standard, no change from HTML5 |
| HTML5The definition of 'HTMLElement.dataset' in that specification. | Recommendation | Snapshot of HTML Living Standard, initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
dataset
|
Chrome
Full support 8 |
Edge
Full support 12 |
Firefox
Full support 6 |
IE
Full support 11 |
Opera
Full support 11 |
Safari
Full support 5.1 |
WebView Android
Full support 4.4 |
Chrome Android
Full support 18 |
Firefox Android
Full support 6 |
Opera Android
Full support 11 |
Safari iOS
Full support 5.1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
Please change the compat macro's paramter to api.HTMLOrForeignElement.dataset after BCD is updated.
See also
- The HTML
data-*class of global attributes. - Using data attributes
Element.getAttribute()andElement.setAttribute()
HTMLOrForeignElement.dataset by Mozilla Contributors is licensed under CC-BY-SA 2.5.