What is the DOM

When your browser loads an HTML page, it builds an internal tree of every element — every <div>, every <h1>, every <button>. This tree is the Document Object Model, or DOM. JavaScript can read, change, add, and remove nodes in this tree, and the browser instantly reflects those changes on screen.

🛒
UrbanCart Example
Every product card on the page is a DOM node

When a customer searches for "headphones" on UrbanCart, the server returns data. JavaScript then creates <div> elements for each product, sets their text and images, and appends them to the page. The customer sees product cards appear — that is DOM manipulation in action.

dom-basics.js Reading the DOM
Output
DOM Manipulation in JavaScript... BODY
Key idea: The DOM is not your HTML file. It is a live, in-memory tree that the browser builds from your HTML. When JavaScript changes the DOM, the page updates instantly — no page reload needed.

querySelector / querySelectorAll

Before you can change an element, you need to select it. The two most important selection methods use CSS selectors — the same syntax you already know from stylesheets.

selectors.js Selecting Elements
Output
<div class="product-card">...</div> 3
querySelector vs. getElementById: Older code uses document.getElementById('myId'). Modern JavaScript prefers querySelector('#myId') because it accepts any CSS selector, not just IDs. Use querySelector for single elements, querySelectorAll when you need a list.

innerHTML vs textContent

Once you have selected an element, you can read or change its content. Two properties handle this — and choosing the right one matters for security.

content.js Setting Content
Output
UrbanCart: Flash Sale <strong>50% OFF</strong> all headphones
Security warning: Never set innerHTML with user-provided data. If a user types <script>alert('hacked')</script> and you inject it with innerHTML, the script runs. This is called Cross-Site Scripting (XSS). Use textContent for user input — it treats everything as plain text.

classList

CSS classes control how elements look. The classList property lets JavaScript add, remove, or toggle classes — so you can change styles without touching the CSS file.

classlist.js Styling Product Cards
Output
product-card in-stock product-card out-of-stock Has featured: true Has featured: false product-card out-of-stock sale highlighted in-stock
🏷️
UrbanCart Example
Toggle "in-stock" and "out-of-stock" badges

When inventory updates arrive, UrbanCart runs card.classList.toggle('out-of-stock', quantity === 0) on each product card. The second argument to toggle() forces add (true) or remove (false), making it perfect for conditional styling.

setAttribute / getAttribute

Attributes live on HTML tags — src, href, alt, data-*. JavaScript can read and write any attribute.

attributes.js Working with Attributes
Output
headphones.jpg 1042 audio 1042 audio
dataset shorthand: Any data-* attribute is accessible via element.dataset. The attribute name is converted to camelCase: data-product-id becomes dataset.productId. This is the preferred way to read and write custom data on elements.

createElement & appendChild

This is where DOM manipulation gets powerful. Instead of writing HTML by hand, you can build elements entirely from JavaScript — perfect for dynamic content like search results or cart items.

create-card.js Building a Product Card
Output
Card created and added to #app
The pattern: createElement builds a detached node. You set its content, classes, and attributes. Then appendChild attaches it to the DOM. The element only appears on screen after you append it to a visible parent.

removeChild & remove

Users remove items from carts. Admins delete products. The DOM needs to lose nodes, too.

remove.js Removing Elements
Output
Card removed with .remove() First child removed with removeChild() Cart display cleared
remove() vs. removeChild(): The remove() method is modern and clean — call it directly on the element. The older removeChild() requires you to call it on the parent and pass the child as an argument. Use remove() in modern code, but know removeChild() because you will see it in existing codebases.

Try It Yourself #1 — Build a product card

Build a product card dynamically: create elements, add classes, set content, and append everything to the page. Edit the code and click Run to see the card appear in the preview.

Try It Yourself — Build a Product Card
Preview

Try It Yourself #2 — Build a product list

Loop through an array of products and create a card for each one. Append all cards to a container. This is exactly how real e-commerce sites render search results.

Try It Yourself — Product List from Array
Preview

Test your knowledge

Question 1 of 5
What does the DOM stand for, and what is it?
Question 2 of 5
Which method selects ALL elements matching a CSS selector?
Question 3 of 5
Why is innerHTML dangerous with user-provided data?
Question 4 of 5
What does el.classList.toggle('active') do?
Question 5 of 5
After calling document.createElement('div'), when does the new element appear on the page?
0
/5
Great job!
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just built DOM elements from scratch.
Now build a full-stack career.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →