Skip to content
Browser & DOMChapter 37 / 47Beginner4 min read

DOM Basics

Learn JavaScript DOM manipulation basics. Find elements with querySelector, change text, classes and attributes, and create new elements.

On this page

1. What is the DOM?

The browser reads your HTML file and turns every tag into a node. These nodes form a tree of parents and children, and the root of that tree is document.

JavaScript updates the page live by changing this tree: changing text, adding items, applying classes. All of it happens through the DOM.

2. Why does it matter?

HTML on its own is static. Without the DOM, you cannot change the page in response to what the user does.

HTML onlyHTML plus DOM and JavaScript
The page stays exactly as writtenThe page changes on user actions
Reload the page to add an itemThe item is added to the list instantly
Changing the theme is not possibleDark mode with a class toggle
The server renders the data to show itBuild elements yourself from API data

3. Where is it used?

Use caseDOM job
Todo listCreate a new li and add it to the list
Dark modeToggle a class on body
Cart countUpdate the badge text
ModalAdd or remove the hidden attribute
React / VueThey use these same DOM APIs under the hood

4. How does it work?

DOM update flow
The browser builds the DOM tree from HTML
JavaScript finds an element with querySelector
It changes the element with a property or method
The browser repaints the screen
TaskMethod
Find one elementdocument.querySelector(".card")
Find all elementsdocument.querySelectorAll("li")
Change textel.textContent = "Hello"
Classel.classList.add / remove / toggle
Attributeel.setAttribute, el.dataset.id
New elementdocument.createElement("li")
Add / removeparent.append(child), el.remove()

5. Code example

HTML:

html
<h1 id="title">My List</h1>
<ul class="todo-list"></ul>
<button id="theme-btn">Theme</button>

JavaScript:

js
const title = document.querySelector("#title");
const list = document.querySelector(".todo-list");
const themeBtn = document.querySelector("#theme-btn");

title.textContent = "Today's tasks";

const items = ["Learn JavaScript", "Walk", "Groceries"];

for (const text of items) {
  const li = document.createElement("li");
  li.textContent = text;
  li.dataset.done = "false";
  list.append(li);
}

themeBtn.addEventListener("click", () => {
  document.body.classList.toggle("dark");
});

console.log(list.children.length);

How does the code work?

LineWhat happens
querySelector("#title")The first element that matches the CSS selector
textContent = ...Sets text safely, no HTML is parsed
createElement("li")A new element in memory, not on the page yet
li.dataset.doneCreates a data-done="false" attribute
list.append(li)Now the element shows up on the page
classList.toggle("dark")Removes the class if present, adds it if not

Interview tip

textContent vs innerHTML: innerHTML parses text as HTML, so user input creates an XSS risk. Always use textContent for user data.

6. Common mistakes

Mistake: Running the script before the DOM exists

Why? If the script is in the head, querySelector returns null and el.textContent throws a TypeError.

Better: Add defer to the script tag or use type module.

Mistake: Putting user input into innerHTML

Why? If someone writes a script or an img onerror tag, that code can run (XSS).

Better: Use textContent, or build elements with createElement.

Mistake: Setting a property directly on the querySelectorAll result

Why? It is a NodeList, so list.style.color does not apply to every element.

Better: Use for...of or forEach and work on each element separately.

Mistake: Updating the page on every item inside a loop

Why? Changing the DOM again and again makes the browser do more work.

Better: Build the items in a DocumentFragment or an array and append them once.

7. What to learn before and after

Suggested order
JavaScript basics
DOM Basics
DOM Events
Event Delegation
Forms and Validation

Final quick revision

PointDetail
DOMA live object tree of the HTML
SelectquerySelector, querySelectorAll
TexttextContent is safe, innerHTML only for trusted HTML
ClassesclassList.add / remove / toggle
Datadata-* attributes through dataset
New elementscreateElement, append, remove