JavaScript DOM
This lesson explains JavaScript DOM in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.
JavaScript DOM Overview
The JavaScript DOM (Document Object Model) is a programming interface that
represents an HTML or XML document as a tree of objects. It allows
JavaScript to read, update, create, delete, and style elements on a web
page dynamically.
DOM manipulation is one of the most important JavaScript skills for frontend
developers. It is used for interactive websites, form validation, dynamic UI
updates, event handling, animations, menus, modals, tabs, accordions, and
real-time content changes.
| DOM Concept | Description | Common Usage |
| Document | Represents the entire web page. | Access page content. |
| Element | Represents an HTML tag. | Headings, buttons, forms. |
| Node | Represents any DOM item. | Elements, text, comments. |
| DOM Tree | Hierarchical structure of the page. | Parent and child elements. |
| DOM Method | Built-in function used to access or change elements. | querySelector, createElement. |
| DOM Event | User or browser action. | Click, submit, input, load. |
JavaScript DOM Example
const title = document.querySelector("#title");
title.textContent = "Learning JavaScript DOM";
const button = document.querySelector("#changeButton");
button.addEventListener("click", function() {
title.style.color = "blue";
title.textContent = "DOM Updated Successfully";
});
This example selects an element using querySelector(), changes
its text using textContent, and updates the page when a button
is clicked.
Top 10 JavaScript DOM Examples
The following examples show common DOM operations used in real-world
frontend development, including selecting elements, changing content,
updating styles, creating elements, and handling user events.
| # | Example | Syntax |
| 1 | Select by ID | document.getElementById("title") |
| 2 | Select First Match | document.querySelector(".card") |
| 3 | Select All Matches | document.querySelectorAll(".item") |
| 4 | Change Text | title.textContent = "New Title"; |
| 5 | Change HTML | box.innerHTML = "<strong>Hello</strong>"; |
| 6 | Change Style | title.style.color = "blue"; |
| 7 | Add Class | box.classList.add("active"); |
| 8 | Create Element | const div = document.createElement("div"); |
| 9 | Append Element | container.appendChild(div); |
| 10 | Add Click Event | button.addEventListener("click", function() { console.log("Clicked"); }); |
Best Practices
- Use
querySelector() and querySelectorAll() for flexible element selection. - Use
textContent when inserting plain text. - Use
innerHTML carefully because it can create security risks with untrusted data. - Use
classList instead of directly replacing the full class attribute. - Use event listeners instead of inline HTML event attributes.
- Cache frequently used DOM elements in variables.
- Avoid too many direct DOM updates inside loops.
- Validate user input before updating the DOM.
Common Mistakes
- Trying to select elements before the page has loaded.
- Using
innerHTML with untrusted user input. - Forgetting that
querySelectorAll() returns a NodeList. - Using the wrong selector syntax.
- Adding multiple duplicate event listeners accidentally.
- Changing inline styles instead of using CSS classes.
- Not checking whether an element exists before updating it.
Key Takeaways
- The DOM represents a web page as a tree of objects.
- JavaScript can select, update, create, and remove DOM elements.
querySelector() selects the first matching element. querySelectorAll() selects all matching elements. textContent is safer for plain text updates. - DOM events make web pages interactive.
Pro Tip
Use textContent for plain text, classList for CSS
changes, and addEventListener() for user interactions. This
keeps DOM code cleaner, safer, and easier to maintain.