Skip to Content
Lesson 1

Introduction to the DOM

Workplace Context

As a front-end developer, one of your primary responsibilities is to create interactive, dynamic web applications. To do this, you need to understand the Document Object Model (DOM), which is the foundation of how browsers interpret and render HTML documents. The DOM provides a structured way to access, update, and manipulate the content of a webpage using JavaScript.

In this lesson, you will explore the basic structure of the DOM, learn how to select elements, and understand why the DOM is a key interface for building interactive web applications.


Learning Objectives

By the end of this lesson, you will be able to:

  • Explain the purpose and structure of the DOM in web development.
  • Identify key elements of the DOM, such as nodes and elements.
  • Use modern JavaScript methods to select elements in the DOM.
  • Inspect the DOM using browser Developer Tools.

What is the DOM?

The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of an HTML or XML document as a tree of objects, allowing JavaScript to interact with and manipulate the content and structure of a webpage.

The DOM acts as a bridge between the HTML content and JavaScript code, enabling dynamic updates to the document’s structure, style, and content without reloading the entire page.

Key Concepts

  1. Nodes: The DOM is structured as a tree of nodes. Nodes can represent elements, attributes, text, or other components of the document.
  2. Elements: Elements are a type of node that represent HTML tags (e.g., <div>, <p>, <a>).
  3. Document Object: The document object is the root of the DOM tree and represents the entire webpage.

Analogy: Think of the DOM as a family tree, where each element is a family member (node) with parent, child, and sibling relationships.

Example: Interactive DOM Visualizer

Take a look at the interactive DOM visualizer below, built by Romain Bohdanowicz. It allows you to view the DOM structure of an HTML document, which you can interact with. Try adding some elements!

Note: Due to the use of iframes, you will need to click “update” to initialize the visualizer. You can also open the visualizer in a new tab using the link above.


Selecting Elements in the DOM

To interact with elements in the DOM, we need to select them using JavaScript. Modern JavaScript offers several methods for element selection:

  1. getElementById(): Selects an element by its id attribute.
  2. getElementsByClassName(): Selects elements by their class name (returns a collection).
  3. querySelector(): Selects the first element that matches a CSS selector.
  4. querySelectorAll(): Selects all elements that match a CSS selector (returns a NodeList).

Example Code Block

Editor
Loading...
Preview

In this example:

  • We use getElementById() to select the heading.
  • We use querySelector() to select the paragraph and button.
  • We add an event listener to update the text content when the button is clicked.

Inspecting the DOM with Developer Tools

One of the best ways to explore and understand the DOM is by using browser Developer Tools.

Steps to Inspect the DOM

  1. Open your browser (e.g., Chrome, Firefox).
  2. Right-click on any element on the page and select Inspect.
  3. Navigate to the Elements tab to see the DOM tree.
  4. Hover over elements in the DOM tree to highlight them on the webpage.

Pro Tip: You can edit the HTML directly in the Developer Tools to see live changes on the page. This is useful for testing and debugging.


Activity: Exploring the DOM

Task

Open the Developer Tools on a webpage of your choice (e.g., https://perscholas.org/). Use the Elements tab to:

  • Inspect the structure of the DOM.
  • Select an element and change its text content directly in the Developer Tools.
  • Use the Console tab to write JavaScript code that changes the content of an element.

Reflection

Discuss your findings with a partner. What elements did you inspect? How did the page change when you modified the content?


Knowledge Check

What is the main purpose of the Document Object Model (DOM)?

  • Select an answer to view feedback.

Which method would you use to select an element by its class name?

  • Select an answer to view feedback.

What does the document object represent in the DOM?

  • Select an answer to view feedback.

Summary

In this lesson, you learned the basics of the DOM, including its structure and how to interact with it using JavaScript. You explored different methods for selecting elements and saw how to inspect the DOM using browser Developer Tools. Mastering these fundamentals will prepare you for more advanced topics in DOM manipulation and dynamic content creation.


Additional Resources