Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
data attributes are a way to store custom data in HTML elements. They start with the data- prefix and allow safely passing information from HTML to JavaScript without creating non-standard attributes.
<div data-user-id="1234" data-role="admin">Hello, this is Hack Frontend team!</div>
data-id)<button data-product-id="42">Buy</button>
const button = document.querySelector("button");
const productId = button.dataset.productId;
console.log(productId); // "42"
All data- attributes become available through element.dataset, and the attribute name converts to camelCase.
Tip:
Use data attributes to store metadata directly in HTML — it's better than creating custom attributes or storing data in classes.