What is the purpose of the data-* attribute in HTML, and give an example of its usage?

Instruction: Describe the data-* attribute and provide a practical example.

Context: This question evaluates the candidate's knowledge of custom data attributes in HTML and their ability to implement them in real-world scenarios.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

data-* attributes attach application-specific values to an element without inventing HTML attributes. JavaScript accesses them through dataset; values are strings.

<button type="button" id="details" data-product-id="42"> Show product details </button> <script> const button = document.querySelector("#details"); console.assert(button.dataset.productId === "42"); button.dataset.productId = "43";...

Related Questions