Skip to content
BENIBIX Learn

Document Object Model (DOM)

Development🌐 Web DevelopmentBrowser Code

Also known as DOM, DOM Tree, querySelector

The browser's live tree of a page. JavaScript can read it and change it.

Think of it like

Think of it like a family tree for a page. Each part has a parent and kids, and code can move them.

Example

When Lucy adds a mug to her cart, JavaScript finds the cart count in the DOM and changes it from 2 to 3.

Why it matters

Every click that changes a page without reloading it goes through the DOM.

Definition

The DOM is the browser's live copy of a web page, held as a tree of parts. When a page loads, the browser turns the HTML into this tree. JavaScript can then find any part, change its text, add new parts or remove old ones. The page on screen updates to match. Every interactive web page works by changing the DOM.

Example code

javascript
document.querySelector("h1").textContent = "Hi Emma";
Find the first heading on the page and change its words.

Common mistake

Changing the DOM many times in a loop. Each change can make the browser redraw, so batch them.

Key takeaways

  1. 1
    A tree of parts. Built from the HTML.
  2. 2
    Live. Change it and the page changes.
  3. 3
    JavaScript's handle. How code reaches the page.

Compare

Learn it properly