/html-dom /Basic
GitHub 4932★

Get, set and remove data attributes

Get the data attribute's value

// Get the `data-message` attribute of the `ele` element
const message = ele.getAttribute('data-message');

// Or
const message = ele.dataset.message;

Set the data attribute's value

ele.setAttribute('data-message', 'Hello World');

// Or
ele.dataset.message = 'Hello World';

Remove the data attribute

ele.removeAttribute('data-message');

// Or
delete ele.dataset.message;

Note that calling delete ele.dataset doesn't remove all data attributes.

See also

Follow me on and to get more useful contents.