JavaScript: dataset

The dataset property reads and changes custom data-* attributes on HTML elements.

What you will learn

Minimal example

<button id="save" data-action="save" data-user-name="Aki">Save</button>
<script>
const button = document.querySelector("#save");
console.log(button.dataset.action); // "save"
console.log(button.dataset.userName); // "Aki"

button.dataset.state = "ready";
</script>

The data- prefix is removed, and each following hyphen changes the next letter to uppercase: data-user-name becomes dataset.userName. Assigning a value updates the corresponding HTML attribute.

Important points