Skip to content

nodeDatasetAttributes

Prefer using element.dataset over getAttribute/setAttribute for data-* attributes.

✅ This rule is included in the browser logical preset.

The dataset property provides a more convenient and cleaner API for working with data-* attributes on DOM elements. It automatically converts between kebab-case attribute names and camelCase property names, making the code more readable and less error-prone.

element.getAttribute("data-foo");
element.setAttribute("data-foo", "bar");
element.removeAttribute("data-foo");
element.hasAttribute("data-foo");

This rule is not configurable.

If you need to support very old browsers that don’t have the dataset property (IE 10 and below), you may need to use the traditional attribute methods. For modern web applications, the dataset property is widely supported and preferred.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.