liveBook · Manning

Data attribute

As explained in Vanilla Web

A custom HTML attribute whose name begins with data- and whose value stores application-specific information on an element.

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

How Vanilla Web covers it

How deep it goes
Defines dataset mapping and works through a binding function, while distinguishing its explicit rendering pass from automatic reactivity.
Examples
Uses product-field binding markers, a disabled button, and a data-status attribute for CSS-selected state.
What it assumes
Assumes basic HTML, JavaScript objects, and DOM querying; introduces dataset access and string-path bindings directly.
What you can do after
Build it from scratch: implement a small dataset-driven binding pass and rerun it when state changes.
How it connects
Connects data attributes to the DOM dataset API, custom elements, templates, CSS selectors, and data binding.

Also explained in

Compare how 4 books explain data attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.