JavaScript: Introduction to the DOM
Last updated: 2026-10-13
The DOM is the only bridge between JavaScript and web pages. Without the DOM, JS would be nothing more than a pure computation language. With the DOM, JS can "see" pages, modify pages, and bring them to life.
1. The DOM Tree and document Object
(1) What Is the DOM?
DOM stands for Document Object Model. When a browser loads HTML, it doesn't store it as plain text — it translates it into an object tree. Each HTML tag becomes an object (called a node), and the nesting relationships between tags become parent-child relationships in the tree.
Think of the DOM as an upside-down tree: at the very top is the root (document), branching down into limbs (html, head, body), and further down into leaves (div, p, span...).
(2) DOM Tree Structure
The DOM tree of a simple HTML page looks roughly like this:
document
└── html
├── head
│ ├── meta
│ └── title
└── body
├── h1
├── p
└── div
└── span
documentis the root node of the DOM tree and the entry point for JS to access the DOMhtmlis the sole child element ofdocument(the document element)headandbodyare the two child elements ofhtml- From there, nesting continues layer by layer to form the complete tree
(3) Node Types
There are three core node types in the DOM:
| Node Type | Description | nodeType Value |
|---|---|---|
| Element node | An HTML tag, e.g. p, div |
1 |
| Text node | Text content inside a tag | 3 |
| Attribute node | A tag's attribute, e.g. class, id |
2 (deprecated for general use) |
<p class="intro">Hello World</p>
In this single line: <p> is an element node, class="intro" is an attribute node, and Hello World is a text node. In everyday development, element nodes are what you'll work with the most.
(4) The document Object
document is the entry point to the DOM — the browser provides this global object automatically. Through it you can:
- Access page elements:
document.getElementById(),document.querySelector() - Create new elements:
document.createElement() - Read and write page content:
document.title,document.body - Listen for page events:
document.addEventListener()
(5) What the DOM Does
JS itself cannot "see" a web page — it can only compute. The DOM provides the "eyes" and "hands":
- Read: Get text, attributes, and styles from the page
- Modify: Change text content, attribute values, and CSS styles
- Add: Create new elements and insert them into the page
- Remove: Remove elements from the page
- Listen: Respond to user clicks, input, and other interactions
Without the DOM, JS and HTML would be two completely separate things. The DOM connects them.
▶ Example: Viewing the DOM Structure
▶ Try it Yourself▶ Example: Exploring the document Object
▶ Try it Yourself▶ Example: Identifying Node Types
▶ Try it Yourself❓ FAQ
children instead of childNodes.📖 Summary
- The DOM is a tree representation of HTML; each tag is a DOM node
documentis the DOM entry point;document.getElementById()finds elements by IDdocument.querySelector()uses CSS selectors for the first match;querySelectorAllfor all- Node types: element nodes (HTML tags), text nodes (content), attribute nodes (tag attributes)
- Core DOM operations: find nodes, read attributes, modify content, add/remove children
📝 Exercises
- Open the browser DevTools (F12), type
console.dir(document)in the Console, and explore the document object's properties and methods. List 5 that you recognize or find interesting. - Write code that gets the
bodyelement of the current page, prints itschildElementCount(number of child elements), then useschildrento loop through all child elements and print each one's tag name. - Explain the difference between "element nodes" and "text nodes". Given
<div>Hello <b>World</b></div>, draw its node tree structure and label the type of each node.