A data attribute stores a small piece of application-specific information on an HTML element. Its name begins with data-, such as data-product-id. JavaScript exposes these attributes through element.dataset: data-user-name becomes dataset.userName. Values are always strings, and changing a dataset property updates the attribute visible in the DOM. section 6.2.11
Use an attribute as a binding marker
<h2 data-bind="product.name"></h2> tells a rendering function where to put text from a state object. The function can read element.dataset.bind, follow the dotted path, and assign the result to textContent. This is an explicit rendering pass: changing the state does nothing until the function runs again. section 13.2.4
function readPath(source, path) {
return path.split(".").reduce(
(value, key) => value?.[key],
source
);
}
function renderBindings(root, state) {
for (const element of root.querySelectorAll("[data-bind]")) {
const value = readPath(state, element.dataset.bind);
element.textContent = value ?? "";
}
for (const element of root.querySelectorAll("[data-bind-attr]")) {
const [name, path] = element.dataset.bindAttr
.split(":")
.map(part => part.trim()); #1
const value = Boolean(readPath(state, path));
element.toggleAttribute(name, value); #2
}
}
This small binding function reads data-bind and data-bind-attr markers, writes text, and toggles a boolean attribute. The paths are strings to look up in state, not JavaScript expressions to evaluate. section 13.2.4
Keep the attribute’s job narrow:
- Use data attributes for IDs, flags, behavior hooks, tiny configurations, or metadata. Keep complex state in memory rather than storing large JSON blobs in markup. section 6.2.11
- For a custom element’s primary configuration, prefer standard attributes such as variant or size. Reserve data-* for information outside the component’s core contract. section 7.9.1
- For simple visual state, write an attribute such as data-status and let CSS selectors choose the presentation. For loops, conditional markup, or frequent updates, use a template-based component or a more explicit render function. section 13.2.10 section 13.2.4



