data-* attributes
Custom attributes designed for page-specific data.
dataset stores small custom values on elements. Templates store reusable HTML that JavaScript can clone safely.
const id = button.dataset.id; const clone = template.content.cloneNode(true); list.append(clone);
DOM & HTML Interaction
Any data-* attribute becomes available through element.dataset. data-user-id becomes element.dataset.userId.
dataset values are strings. Convert numbers and booleans yourself when you need real types.
The template element keeps inert markup in the document. JavaScript can clone it, fill it and insert it when needed.
Custom attributes designed for page-specific data.
Camel-cased JavaScript access to data attributes.
Reusable HTML that does not render until cloned.
Copies template content for insertion.
Examples
const clone = template.content.cloneNode(true);
clone.querySelector("[data-title]").textContent = record.title;
list.append(clone);
card.dataset.record = JSON.stringify(largeRecord); // Keep complex state in JavaScript data structures instead.
Code patterns
These patterns are the DOM moves you use constantly: select, read, change, create, navigate and render.
data-item-id becomes dataset.itemId.
const id = button.dataset.itemId;
Values become strings in HTML attributes.
card.dataset.state = "selected";
Clone template content before editing it.
const clone = template.content.cloneNode(true);
clone.querySelector("[data-title]").textContent = title;
dataset values are always strings.
const count = Number(button.dataset.count);
Rules that matter
DOM APIs are powerful because they affect the live page. Use them with stable hooks, clear state and safe text updates.
Use it for IDs, states and simple values.
dataset values are strings.
They keep HTML structure readable.
Do not mutate template.content as the final rendered node.
Use textContent for dynamic labels.
data-* attributes are visible to users.
Production thinking
dataset and template give plain JavaScript a clean way to connect markup, state and rendering.
Templates should contain semantic markup from the start so every clone is accessible.
Production templates are useful for small islands of interactivity without a full framework.
Template content is inert until cloned, so do not hide critical crawlable text only inside templates.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain the DOM operation and its failure path, you are ready to use it in real UI code.
Senior audit upgrade
Prefer textContent, createElement, templates and replaceChildren for normal UI rendering. Reach for innerHTML only when the markup is controlled and the risk is understood.
const item = document.createElement("li");
item.textContent = userInput;
list.replaceChildren(item);
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Render a small list from data using createElement, textContent and replaceChildren.
What would happen if the data contained HTML-looking text?