Skip to content

Prefer using .dataset on DOM elements over calling attribute methods ​

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

Use .dataset on DOM elements over getAttribute(…), .setAttribute(…), .removeAttribute(…) and .hasAttribute(…).

Fail ​

js
const unicorn = element.getAttribute('data-unicorn');
js
element.setAttribute('data-unicorn', '🦄');
js
element.removeAttribute('data-unicorn');
js
const hasUnicorn = element.hasAttribute('data-unicorn');

Pass ​

js
const {unicorn} = element.dataset;
js
element.dataset.unicorn = '🦄';
js
delete element.dataset.unicorn;
js
const hasUnicorn = Object.hasOwn(element.dataset, 'unicorn');
js
const foo = element.getAttribute('foo');
js
element.setAttribute('not-dataset', '🦄');
js
element.removeAttribute('not-dataset');
js
const hasFoo = element.hasAttribute('foo');

Released under the Apache License 2.0.