Skip to main content

Skillber v1.0 is here!

Learn more

What is the DOM?

Checking access...

The Document Object Model (DOM) is a programming interface for HTML documents. It represents the page as a tree of objects that can be manipulated with JavaScript.

From HTML to DOM

When the browser loads an HTML file, it parses the markup and builds a DOM tree:

<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Hello!</h1>
<p>This is a paragraph.</p>
</body>
</html>

Becomes this tree structure:

document
└── html
├── head
│ └── title
│ └── "My Page"
└── body
├── h1
│ └── "Hello!"
└── p
└── "This is a paragraph."

Every element, attribute, and text node is represented as an object in this tree. Each object has properties (like textContent, style) and methods (like appendChild, remove).

The document Object

The document object is the entry point to the DOM. It represents the entire web page and is available globally in all browsers:

// Access the document title
console.log(document.title); // "My Page"
// Get the URL
console.log(document.URL); // "https://example.com/page"
// Get the page's body element
console.log(document.body); // <body>...</body>
// Get the head element
console.log(document.head); // <head>...</head>

DOM Tree Terminology

document
└── html (root element)
├── head
│ ├── title
│ │ └── text node: "My Page"
│ └── meta (attribute: charset="utf-8")
└── body
├── h1
│ └── text node: "Hello!"
└── p (element node)
└── text node: "This is a paragraph."
  • Root node: Always document
  • Element nodes: HTML tags (html, body, h1, p)
  • Text nodes: The text content inside elements
  • Attribute nodes: Attributes on elements (like class, id, src)
  • Parent/Child/Sibling: The tree follows family tree terminology
    • html is the parent of head and body
    • head and body are siblings
    • head and body are children of html

Every node has properties that let you navigate the tree:

const body = document.body;
// Parent
console.log(body.parentNode); // <html>
// Children
console.log(body.children); // HTMLCollection of child elements
console.log(body.childNodes); // NodeList including text nodes
// First/last child
console.log(body.firstElementChild); // first element child
console.log(body.lastElementChild); // last element child
// Siblings
const h1 = document.querySelector("h1");
console.log(h1.nextElementSibling); // <p> after the h1
console.log(h1.previousElementSibling); // null (h1 is first in body)

Node Types vs Element Properties

PropertyIncludesReturns
parentNodeAny node type (including document)Node
parentElementOnly element nodesElement or null
childNodesAll node types (text, comment, element)NodeList
childrenOnly element nodesHTMLCollection (live)
firstChildAny node typeNode
firstElementChildOnly elementsElement
nextSiblingAny node typeNode
nextElementSiblingOnly elementsElement

Exploring the DOM in Console

Open your browser’s DevTools (F12 or Ctrl+Shift+I) and go to the Console tab:

// See the whole DOM as a tree
console.dir(document);
// Inspect specific elements
console.dir(document.body);
// Check node types
console.log(document.nodeType); // 9 (DOCUMENT_NODE)
console.log(document.body.nodeType); // 1 (ELEMENT_NODE)
// See the tag name
console.log(document.body.tagName); // "BODY"

Node type constants:

ConstantValueRepresents
ELEMENT_NODE1HTML elements
ATTRIBUTE_NODE2Attributes
TEXT_NODE3Text content
COMMENT_NODE8HTML comments
DOCUMENT_NODE9The document

The DOM is Live

The DOM is live — changes in JavaScript immediately reflect in the browser:

// Type this in the console on any page:
document.title = "New Title!";
// Watch the browser tab title change instantly
document.body.style.backgroundColor = "lightblue";
// The page background changes immediately

Common Misconceptions

1. The DOM is not the HTML source

The DOM is a live representation that can differ from the original HTML source:

// JavaScript can add elements that never existed in the HTML
const div = document.createElement("div");
div.textContent = "I was added by JS!";
document.body.appendChild(div);

2. The DOM is not JavaScript

The DOM is a browser API, not part of JavaScript itself. JavaScript is just one language that can interact with it. (Python, Rust, and other languages can also manipulate the DOM when compiled to WebAssembly.)

3. innerHTML vs the DOM

// Setting innerHTML — browser re-parses HTML string
element.innerHTML = "<p>New paragraph</p>";
// DOM methods — more explicit and slightly faster
const p = document.createElement("p");
p.textContent = "New paragraph";
element.appendChild(p);

Quick Reference

ConceptDescription
DOMTree representation of HTML
documentEntry point to the DOM
Element nodeRepresents an HTML tag
Text nodeText content inside elements
Parent/Child/SiblingTree relationship terms
Live DOMChanges reflect immediately

Practice Exercises

  1. Open any webpage in your browser console. Use document.body.children to list all direct children of the body. How many are there?

  2. Navigate the DOM tree from document.body: find the first child element, then its next sibling, then the parent of that sibling. Which elements did you visit?

  3. Change the page’s document.title to your name. Then inspect document.body.firstElementChild — what is it?