Skip to content

DOM Tree

The DOM represents an HTML document as a tree of nodes, where each node corresponds to an element, text, attribute, or comment.

flowchart TD
A["document"]
B["<html>"]
C["<head>"]
D["<body>"]
E["<title>"]
F["<h1>"]
G["<p>"]
A --> B
B --> C
B --> D
C --> E
D --> F
D --> G
// The DOM starts with the document object
console.log(document); // The entire HTML document
console.log(document.head); // <head> element
console.log(document.body); // <body> element
console.log(document.title); // Page title
Node TypeExample
Element node<div>, <p>, <h1>
Text node”Hello World” inside an element
Comment node<!-- comment -->
Document nodeThe document object itself
  • DOM is a tree of nodes representing the HTML document
  • document is the root entry point
  • Node types: element, text, comment, document
  • Each element node can have child nodes