Document
The root object for the current page.
The DOM is the live object model of an HTML document. JavaScript uses it to read, change, create and remove what users see.
const title = document.querySelector("h1");
title.textContent = "Updated by JavaScript";
document.body.classList.add("is-ready");
DOM & HTML Interaction
When the browser loads HTML, it builds the Document Object Model. Every element becomes an object with properties, methods and relationships to other nodes.
The DOM is live. If you change an element with JavaScript, the page changes immediately. That is powerful, but it also means DOM code should be deliberate and predictable.
Think of the DOM as the connection layer between your data and the visible interface. Good DOM code respects structure, accessibility and performance.
The root object for the current page.
An HTML element represented as a JavaScript object.
Parent, child and sibling relationships between nodes.
Changes to DOM objects update the rendered page.
Examples
const status = document.querySelector("[data-status]");
if (status) {
status.textContent = "Ready";
}
document.querySelector("[data-status]").textContent = "Ready";
// This crashes when the element is missing.
Code patterns
These patterns are the DOM moves you use constantly: select, read, change, create, navigate and render.
querySelector returns the first matching element or null.
const heading = document.querySelector("h1");
heading.textContent = "DOM ready";
querySelectorAll returns a static NodeList.
const cards = document.querySelectorAll(".card");
cards.forEach(card => card.classList.add("ready"));
createElement gives you a new element object.
const item = document.createElement("li");
item.textContent = "New item";
list.append(item);
Elements know their parent and children.
const button = document.querySelector("button");
const panel = button.closest(".panel");
Rules that matter
DOM APIs are powerful because they affect the live page. Use them with stable hooks, clear state and safe text updates.
data-* hooks are often safer than styling classes.
A missing element should not crash the whole interface.
It avoids accidental HTML injection.
Update the smallest useful part of the page.
DOM changes should preserve labels, focus and states.
Large DOM updates can become expensive.
Production thinking
Most real JavaScript work eventually touches the DOM. If this layer is messy, the interface becomes fragile.
DOM changes should keep accessible names, focus order and visible status messages clear.
Production DOM code should be scoped, defensive and easy to trace back to a component or feature.
Important page content should exist in crawlable HTML or render reliably when JavaScript runs.
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);