Skip to content

Files

Latest commit

 

History

History
23 lines (17 loc) · 613 Bytes

prefer-dom-node-dataset.md

File metadata and controls

23 lines (17 loc) · 613 Bytes

Pattern: DOM data-* attribute access via getAttribute

Issue: -

Description

The dataset property provides a cleaner way to access data-* attributes on DOM elements compared to using getAttribute(), setAttribute(), removeAttribute(), or hasAttribute().

Examples

Example of incorrect code:

element.setAttribute("data-unicorn", "🦄");
element.getAttribute("data-unicorn");
element.removeAttribute("data-unicorn");

Example of correct code:

element.dataset.unicorn = "🦄";
const value = element.dataset.unicorn;
delete element.dataset.unicorn;