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.
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.
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.
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.
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.
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.
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.
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() 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 #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.
Test your knowledge
innerHTML dangerous with user-provided data?el.classList.toggle('active') do?document.createElement('div'), when does the new element appear on the page?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 →