The DOM (Document Object Model) is the browser's live, tree-shaped representation of your HTML page, and JavaScript can read and change it to update what the user sees. When the browser loads a page, every element, attribute and piece of text becomes an object (a node) that your script can find, edit, add or remove. Changing those nodes changes the page instantly without a reload.
This guide explains what the DOM is, how to select elements, and how to change text, attributes, classes and structure safely.
The browser parses your HTML and builds a tree. The document object is the root, and every tag becomes an element node below it.
For this page, document contains html, which contains body, which contains the h1 and the ul, and the ul has two li children. Text inside tags is also a node. The tree has relationships such as parent, child and sibling, and you can walk it with properties like parentElement, children and nextElementSibling.
The DOM is not your HTML file. It is a live object built from it, and scripts change the DOM, not the file on the server. If you view the page source after a change, the source stays the same, but the inspector in your browser developer tools shows the updated DOM.
Before you can change something you need a reference to it. The two modern methods take a CSS selector.
querySelector returns the first match or null. querySelectorAll returns a static NodeList of all matches, which you can loop with for...of or forEach. Older methods such as getElementById and getElementsByClassName still work, and getElementById is a fast, clear choice when you have an id.
Always make sure the script runs after the elements exist. Put <script type="module" src="app.js"> in the page (module scripts are deferred automatically), or add the defer attribute to a classic script. Otherwise querySelector returns null, and the next line throws a TypeError.
Once you have an element, update it through its properties.
| Task | Code | Notes |
|---|---|---|
| Set plain text | el.textContent = "Hi" | Safe, never parses HTML |
| Read or set an attribute | el.getAttribute("href"), el.setAttribute("href", url) | Works for any attribute |
| Add, remove, toggle a class | el.classList.add("on"), .remove, .toggle | Preferred over editing className |
| Inline style | el.style.color = "red" | Prefer classes for real styling |
| Form value | input.value | Current text of an input |
| Hide an element | el.hidden = true | Uses the built-in hidden attribute |
Prefer toggling CSS classes over writing many inline styles. Your stylesheet stays in charge of the design, and the script only decides which state applies.
You can build new elements in JavaScript and add them to the page.
createElement makes the element in memory, and nothing is visible until you insert it. append adds at the end, prepend at the start, and before and after insert next to an element. remove() deletes an element from the page. To add many items, build them in a loop and insert them once, or use a DocumentFragment, to avoid repeated layout work.
innerHTML reads or sets the HTML inside an element as a string. It is convenient but dangerous when the string includes user input.
Inserting untrusted data with innerHTML is a classic cross-site scripting (XSS) hole. Use textContent for text, build elements with createElement, or sanitise the HTML with a trusted library or the browser's Sanitizer API where it is supported. Reserve innerHTML for markup you wrote yourself.
This page counts clicks and writes the result into the DOM.
The script selects the button, listens for a click, then updates the text and a class each time.
No. The DOM is a web platform API defined for browsers, and JavaScript is the language that uses it. Node.js has no DOM by default.
A node is any item in the tree, including text and comments. An element is a node that comes from an HTML tag. Properties like children list only elements, while childNodes includes all nodes.
querySelector returning null?The selector does not match, or the script ran before the element was parsed. Check the selector in the inspector and load the script with defer or as a module.
getElementById faster than querySelector?Slightly, but the difference rarely matters. Choose the one that makes your code clearer.
The DOM is the browser's live tree of objects built from your HTML, and JavaScript changes the page by changing that tree. Select elements with querySelector and querySelectorAll, update them with textContent, attributes and classList, and add or remove nodes with createElement, append and remove. Prefer textContent over innerHTML for anything that comes from users, and run scripts after the elements exist.