0

What are data-* attributes used for?

author
pranav m
easy
3
96

Answer
  • Custom attributes to store extra information on HTML elements.
  • Accessible in JS with .dataset.
  • Useful for metadata not meant for users.

Example

<button data-user-id="42" data-role="admin">Edit User</button>

<script>
let btn = document.querySelector("button");
console.log(btn.dataset.userId); // "42"
console.log(btn.dataset.role); // "admin"
</script>

Click to Reveal Answer

Tap anywhere to see the solution

Revealed

Comments0