Accessing data-* with dataset in JavaScript
Read and write custom data attributes via the dataset API.
What is the dataset API?
Every HTML element exposes its data-* attributes via the dataset property — a DOMStringMap that maps attribute names to values without requiring getAttribute() calls.
Attribute to Property Mapping
data-user-id becomes el.dataset.userId. Hyphens in attribute names are converted to camelCase in the dataset property. Accessing data-item-count uses el.dataset.itemCount.
All lessons in this course
- data-* Syntax and Naming Conventions
- Accessing data-* with dataset in JavaScript
- Using data-* with CSS attribute selectors
- Real-world Patterns Tooltips State Tracking