The DOM & node tree
Understand the idea
A selector returns even though the element appears in your HTML. The selector ran before the browser had created that part of the document.
A live tree for the page
The HTML file is the written starting point; the DOM is the browser's current representation of it. If JavaScript adds a paragraph, that paragraph appears in the tree even though the HTML on disk has not changed. This is how a page can update after loading.
Nodes and elements are different
Elements are one kind of node. Text, comments, and the document itself are nodes too. Whitespace can create text nodes, which is why firstChild is not guaranteed to be an element. children and firstElementChild are useful when you specifically need elements.
A node is one item in the tree. Elements such as paragraphs are nodes, but text and comments are nodes too. A parent contains child nodes; nodes with the same parent are siblings. You can use these relationships to work on a specific part of a page instead of searching the whole document every time.
| API or value | Purpose | Result to expect |
|---|---|---|
| document | Entry point to the page’s DOM | The document node |
| querySelector(selector) | Find the first matching element | An element or null |
| querySelectorAll(selector) | Find current matching elements | A static NodeList; not a live array |
| createElement(tag) | Create an unattached element | A new element, not yet visible |
| append(node) | Attach a node as a child | The tree and visible page can change |
| window / location / history | Work with the surrounding browser | Browser capabilities, not HTML elements |
Finding a node safely
A selector searches the tree as it exists at that moment. querySelector returns the first matching element or . querySelectorAll gives a static NodeList of matches; it does not automatically gain elements added later. Some older collection return live collections, so check which behavior you have.
Changing the tree
Changing textContent inserts text rather than parsing it as HTML. That makes it the right default for untrusted display text. Creating an element does not attach it to the tree; append it to a parent when it should become part of the document.
The document and its browser
The browser environment extends beyond the DOM. window, location, history, and timers describe the surrounding browser, often grouped as the BOM. Run DOM-dependent code after its required elements exist, for example through a deferred script or DOMContentLoaded, and still check selectors that may legitimately have no match.
BOM is a common label for browser features around the document, such as location, history, and the window itself. It is not a second page tree. Server-side JavaScript may have no browser window at all. In the live example, createElement makes an element, textContent gives it safe text, and append connects it to the visible document.
Mental model
FOLLOW THE VALUE
The node exists but is not yet in the document tree.
01 / 03Trace the transformation
Live example
This example creates its own paragraph, so it does not depend on unseen HTML. nodeType 1 identifies an element node.
Your console output appears here.
Guided challenge
Select the created element
Match the selector to the p element that the code adds to the document.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
Ready to learn 1
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.