Understanding the DOM (Document Object Model)
The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a webpage as a tree of objects, allowing developers to manipulate content, structure, and styles dynamically.
Key concepts
Document
The root object of the DOM tree, representing the entire HTML document.
Nodes
Represent elements, attributes, or text in the DOM tree.
DOM Tree
A hierarchical representation of the document structure.
Examples
Example of a Simple DOM Tree:
<html>
<body>
<h1 id="title">Hello World</h1>
<p class="content">This is a paragraph.</p>
</body>
</html>The DOM tree looks like this:
Document
└── <html>
└── <body>
├── <h1 id="title">Hello World</h1>
└── <p class="content">This is a paragraph.</p>Accessing the DOM in JavaScript:
// Accessing elements
const title = document.getElementById('title');
const paragraphs = document.querySelectorAll('.content');
// Manipulating content
title.textContent = 'Welcome to the DOM!';Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus