dom-node-dataset
April 17, 2026 ยท View on GitHub
๐ Enforce consistent style for DOM element dataset access.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ง This rule is automatically fixable by the --fix CLI option.
Use .dataset on DOM elements over getAttribute(โฆ), .setAttribute(โฆ), .removeAttribute(โฆ) and .hasAttribute(โฆ).
Examples
// โ
const unicorn = element.getAttribute('data-unicorn');
// โ
const {unicorn} = element.dataset;
// โ
element.setAttribute('data-unicorn', '๐ฆ');
// โ
element.dataset.unicorn = '๐ฆ';
// โ
element.removeAttribute('data-unicorn');
// โ
delete element.dataset.unicorn;
// โ
const hasUnicorn = element.hasAttribute('data-unicorn');
// โ
const hasUnicorn = Object.hasOwn(element.dataset, 'unicorn');
// โ
const foo = element.getAttribute('foo');
// โ
element.setAttribute('not-dataset', '๐ฆ');
// โ
element.removeAttribute('not-dataset');
// โ
const hasFoo = element.hasAttribute('foo');
Options
preferAttributes
Type: boolean
Default: false
When true, enforces the opposite direction: prefer getAttribute(โฆ) / setAttribute(โฆ) / removeAttribute(โฆ) / hasAttribute(โฆ) over named .dataset access โ covering reads, writes, delete, simple destructuring, and existence checks (in, Object.hasOwn, .hasOwnProperty()). Whole-object reads (const data = element.dataset) and inherited members (element.dataset.toString) are not flagged. This can be useful for greppability when data attributes are also referenced in CSS/HTML.
// eslint unicorn/dom-node-dataset: ["error", {"preferAttributes": true}]
// โ
const unicorn = element.dataset.unicorn;
element.dataset.unicorn = '๐ฆ';
delete element.dataset.unicorn;
'unicorn' in element.dataset;
// โ
const unicorn = element.getAttribute('data-unicorn');
element.setAttribute('data-unicorn', '๐ฆ');
element.removeAttribute('data-unicorn');
element.hasAttribute('data-unicorn');