Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLElement.dataset — accessing data-* attributes

HTMLElement.dataset exposes an element's data-* attributes as a DOMStringMap. It lets you read, write, and delete string data, mapping names such as data-user-id to userId.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

element.dataset.key
 element.dataset.key = value;
 element.dataset[key] = value;
 delete element.dataset.key;

HTMLElement.dataset returns an element-associated DOMStringMap, not an independent data copy. Property changes update the data-* attributes and vice versa. The dataset property itself is read-only; you modify its properties.

Name mapping removes the data- prefix; a hyphen before an ASCII lowercase letter is removed and that letter becomes uppercase: data-user-id → userId. Other characters remain unchanged. When writing, ASCII uppercase letters become a hyphen followed by lowercase. Writing a key containing a hyphen before an ASCII lowercase letter, such as dataset['user-id'], throws SyntaxError.

Values are strings: convert numbers explicitly. Assigning null stores the text "null", rather than removing the attribute. Use delete element.dataset.userId to remove it. A missing userId key yields undefined.

This is an HTML Living Standard interface, not a feature of a particular ECMAScript edition. Current browser engines support it; having Node 24 in a repository does not establish DOM availability.

Examples

const element = document.createElement('div');
element.setAttribute('data-user-id', '42');
console.log(element.dataset.userId);
element.dataset.userId = Number(element.dataset.userId) + 1;
console.log(element.getAttribute('data-user-id'));
console.log(typeof element.dataset.userId);
delete element.dataset.userId;
console.log(element.hasAttribute('data-user-id'));

Result

42
43
string
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources