The Document Object Model (DOM) is the browser’s live, in-memory tree representing a document. Its nodes include elements, text, and comments, with parent, child, and sibling relationships. JavaScript can query and change those objects to update the interface without a UI library. The DOM is not the HTML source: it begins with parsed HTML and can change at runtime. section 6.0 section 6.2
const title = document.createElement("h1");
title.textContent = "Hello DOM";
document.body.prepend(title);
This small example creates an element, sets plain text, and inserts it at the start of the document body. section 6.2
Use the native API according to the job:
- Find elements with selectors, then traverse parents, children, or siblings. `children` excludes text and comments; `childNodes` includes them. A collection may be live or a static snapshot, so check before iterating while changing the tree. section 6.2.5 section 6.2
- Set plain text with `textContent`; create elements for structure. Use `append`, `before`, or `replaceWith` to arrange nodes. Moving an existing node relocates it rather than copying it, preserving its state and listeners. section 6.2 section 6.2.10
- Respond to UI events with listeners, and change presentation through classes or data attributes while leaving visual rules to CSS. Use `innerHTML` for controlled templates rather than as the default way to insert dynamic content. section 6.4
Render data from a template
For a shopping-cart list, clone an HTML `template` for each item, fill its name, quantity, and price through selected elements’ `textContent`, append the clones to a list, then replace the component’s children with that list. This reuses parsed structure instead of constructing HTML strings for each item. section 8.3
DOM changes happen synchronously, but the browser does not repaint after every property assignment; rendering waits until JavaScript yields. Even so, many uncoordinated edits can add layout and painting work. Build several nodes in a fragment or detached container before attaching them. For components, distinguish the page DOM from a shadow DOM: without a shadow root, component and page styles can affect each other. section 6.1 section 6.2.10 section 7.8.2











