DOM HTMLElement.dataset Property
The dataset property gives easy access to an element's data-* attributes, as an object with camelCase property names.
Description
The dataset property provides read and write access to the element's custom data attributes (data-*). The data- prefix is removed and dashed names become camelCase: data-user-id is dataset.userId. Values are always strings. Adding a property creates the attribute, and delete removes it.
Data attributes are a good way to pass information from your HTML to your JavaScript, such as IDs, settings or state, without inventing non-standard attributes. They're also visible to CSS, so [data-state="open"] selectors can style elements based on the same values your script changes.
Because values are strings, convert them when needed: Number(el.dataset.count) or el.dataset.enabled === 'true'. Store small pieces of data only; for larger objects, keep the data in JavaScript and store a key in the attribute.
Syntax
element.dataset
element.dataset.name = valueValue
A DOMStringMap of the element's data-* attributes, with names in camelCase.
Example
Browser Support
The following information will show you the current browser support for the HTMLElement.dataset property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





