HTMLElement: dataset property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thedataset read-only propertyof theHTMLElement interface provides read/write access tocustom data attributes(data-*) on elements. It exposes a map of strings(DOMStringMap) with an entry for eachdata-* attribute.
Note:Thedataset property itself can be read, but not directly written.Instead, all writes must be to the individual properties within thedataset, which in turn represent the data attributes.
In this article
Value
An HTMLdata-* attribute and its corresponding DOMdataset.property modify their shared name according to wherethey are read or written:
- In HTML
The attribute name begins with
data-. It can contain only letters,numbers, dashes (-), periods (.), colons (:),and underscores (_). AnyASCII capital letters (AtoZ) are converted to lowercase.- In JavaScript
The property name of a custom data attribute is the same as the HTML attributewithout the
data-prefix. Single dashes (-) are removed, and the next ASCIIcharacter after a removed dash is capitalized to form the property's camel-cased name.
Details and examples of converting between the HTML and JavaScript forms is described in more detail in the next section.
In addition to the information below, you'll find a how-to guide for using HTML dataattributes in our articleUsing data attributes.
Name conversion
dash-styletocamelCaseconversionA custom data attribute name is transformed to a key for the
DOMStringMapentry by the following:- Lowercase all ASCII capital letters (
AtoZ); - 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.
- Lowercase all ASCII capital letters (
camelCasetodash-styleconversionThe opposite transformation, which maps a key to an attribute name, uses thefollowing:
- Restriction: Before transformation, a dashmust not beimmediately 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 dashmust not beimmediately followed by an ASCII lowercase letter
For example, adata-abc-def attribute corresponds todataset.abcDef.
Accessing values
- Attributes can be set and read by the camelCase name/key as an object property ofthe 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. Note that this will walk theprototype chain ofdatasetand may be unsafe if you have external code that may pollute the prototype chain. Several alternatives exist, such asObject.hasOwn(element.dataset, 'keyname'), or just checking ifelement.dataset.keyname !== undefined.
Setting values
When the attribute is set, its value is always converted to a string.For example:
element.dataset.example = nullisconverted intodata-example="null".To remove an attribute, you can use the
deleteoperator:delete element.dataset.keyname.
Examples
<div data-id="1234567890" data-user="carinaanand" data-date-of-birth> Carina Anand</div>const el = document.querySelector("#user");// el.id === 'user'// el.dataset.id === '1234567890'// el.dataset.user === 'carinaanand'// el.dataset.dateOfBirth === ''// set a data attributeel.dataset.dateOfBirth = "1960-10-03";// Result on JS: el.dataset.dateOfBirth === '1960-10-03'// Result on HTML: <div data-id="1234567890" data-user="carinaanand" data-date-of-birth="1960-10-03">Carina Anand</div>delete el.dataset.dateOfBirth;// Result on JS: el.dataset.dateOfBirth === undefined// Result on HTML: <div data-id="1234567890" data-user="carinaanand">Carina Anand</div>if (el.dataset.someDataAttr === undefined) { el.dataset.someDataAttr = "mydata"; // Result on JS: 'someDataAttr' in el.dataset === true // Result on HTML: <div data-id="1234567890" data-user="carinaanand" data-some-data-attr="mydata">Carina Anand</div>}Specifications
| Specification |
|---|
| HTML> # dom-dataset-dev> |
Browser compatibility
See also
- The HTML
data-*classof global attributes - Using data attributes
Element.getAttribute()andElement.setAttribute()