Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 16 / 34

The DOM & node tree

01

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.

Finding and changing the document tree
API or valuePurposeResult to expect
documentEntry point to the page’s DOMThe document node
querySelector(selector)Find the first matching elementAn element or null
querySelectorAll(selector)Find current matching elementsA static NodeList; not a live array
createElement(tag)Create an unattached elementA new element, not yet visible
append(node)Attach a node as a childThe tree and visible page can change
window / location / historyWork with the surrounding browserBrowser 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.

02

Mental model

FOLLOW THE VALUE

createElement
detached p
no parent

The node exists but is not yet in the document tree.

01 / 03Trace the transformation

03

Live example

This example creates its own paragraph, so it does not depend on unseen HTML. nodeType 1 identifies an element node.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Select the created element

Match the selector to the p element that the code adds to the document.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

Ready to learn
1

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does querySelector return when nothing matches?
    02Does createElement immediately display an element?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson