Pattern: DOM data-*
attribute access via getAttribute
Issue: -
The dataset
property provides a cleaner way to access data-*
attributes on DOM elements compared to using getAttribute()
, setAttribute()
, removeAttribute()
, or hasAttribute()
.
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;