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');