The DOM Explained: Manipulating Web Pages
SkillVeris Team
Engineering Team

The DOM (Document Object Model) is a tree of JavaScript objects that represents your HTML, letting scripts read and change the page after it loads.
In this guide, you'll learn:
- You select elements with methods like querySelector and querySelectorAll, then read or update their content, attributes, and styles.
- Event listeners let your code respond to clicks, input, and other user actions in real time.
- Creating and inserting elements with createElement and appendChild lets you build page content dynamically.
- Batching DOM changes and using event delegation keeps interactive pages fast.
1What Is the DOM?
The DOM, or Document Object Model, is a live, tree-shaped representation of your HTML that the browser builds in memory. Every tag becomes a node object, and JavaScript can read, change, add, or remove those nodes to update what the user sees — without reloading the page. In short, the DOM is the bridge between your static HTML and dynamic behaviour.
When a page loads, the browser parses the HTML into this tree, with the document object at the root and elements nested as parents and children. Any change you make to the tree is reflected on screen almost immediately, which is what makes interactive web pages possible.
2Selecting Elements
Before you can change anything, you have to find it. The modern way to select elements uses CSS-selector syntax, so if you know CSS you already know most of what you need. querySelector returns the first match, and querySelectorAll returns all matches as a static list.
- document.querySelector('.card') # first element with class card
- document.querySelectorAll('li') # all list items as a NodeList
- document.getElementById('menu') # fast lookup by id
- element.closest('.container') # nearest matching ancestor
💡Pro Tip
querySelector and querySelectorAll accept any CSS selector, so you rarely need the older getElementsBy methods. Learn one flexible tool instead of five narrow ones.
3Changing Content, Attributes, and Styles
Once you have an element, you can change almost anything about it. Text and HTML content, attributes like href or src, CSS classes, and inline styles are all editable through simple properties and methods. Prefer textContent over innerHTML when inserting plain text to avoid accidentally running markup.
- el.textContent = 'Saved' # safest way to set text
- el.classList.add('active') # toggle, add, or remove classes
- el.setAttribute('href', '/next') # change any attribute
- el.style.display = 'none' # set an inline style
textContent vs innerHTML
innerHTML parses its string as HTML, which is powerful but risky with untrusted input because it can inject scripts. textContent treats everything as plain text and is both safer and faster for simple updates.
4Traversing the Tree
Because the DOM is a tree, every node knows its neighbours, and you can move between them without re-querying. From any element you can reach its parent, its children, and its siblings, which is handy when an event handler needs the element next to the one that was clicked.
Traversal keeps code resilient. Instead of hardcoding a selector for a nearby element, you navigate relative to the node you already have, so the logic still works if class names or ids change around it.
- el.parentElement # the node directly above
- el.children # element children as a collection
- el.nextElementSibling # the element after this one
- el.closest('form') # nearest ancestor matching a selector
5Responding to Events
Interactivity comes from events — clicks, typing, scrolling, and more. You attach a listener with addEventListener, passing the event name and a callback that runs whenever the event fires. The callback receives an event object with details like which element was clicked and what key was pressed.
This pattern is the heart of every interactive page. A button that opens a menu, a form that validates as you type, a gallery that responds to swipes — all of them are event listeners updating the DOM in response to user actions.
- button.addEventListener('click', () => openMenu())
- input.addEventListener('input', e => console.log(e.target.value))
- form.addEventListener('submit', e => e.preventDefault())
6Creating and Removing Elements
Beyond editing existing elements, you can build new ones from scratch. createElement makes a node, you configure it, and then you insert it into the tree with methods like appendChild or append. Removing is just as direct with remove. This is how lists grow, notifications appear, and content loads on demand.
- const li = document.createElement('li') # make a node
- li.textContent = 'New item' # configure it
- list.appendChild(li) # insert into the tree
- li.remove() # take it back out
7Best Practices for DOM Performance
The DOM is fast, but careless updates can make a page feel sluggish. A few habits keep interactions smooth even on modest devices, and they become more important as pages grow.
- Batch changes: build elements off-screen or in a fragment, then insert once instead of many times.
- Use event delegation: attach one listener to a parent rather than hundreds to children.
- Cache selections: store a queried element in a variable instead of re-querying in a loop.
- Prefer textContent over innerHTML for plain text to stay safe and fast.
- Avoid reading and writing layout in a tight loop, which forces repeated reflows.
⚠️Watch Out
Inserting user-supplied strings with innerHTML can open a cross-site scripting hole. Sanitise input or use textContent whenever the content is not fully trusted.
8Key Takeaways
The DOM becomes intuitive once these core ideas click.
- The DOM is a live object tree representing your HTML that JavaScript can change on the fly.
- Select with querySelector and querySelectorAll using familiar CSS selectors.
- Update content, attributes, classes, and styles through element properties and methods.
- Respond to user actions with addEventListener and the event object.
- Batch updates and delegate events to keep interactive pages fast.
9Frequently Asked Questions
Q: What is the difference between the DOM and HTML? A: HTML is the static text you write; the DOM is the live in-memory tree the browser builds from that HTML. JavaScript changes the DOM, and the browser re-renders the page to match.
Q: Should I use innerHTML or textContent? A: Use textContent for plain text — it is safer and faster. Use innerHTML only when you intentionally need to insert markup, and never with untrusted input, because it can run injected scripts.
Q: What is event delegation? A: Event delegation means attaching a single listener to a common parent and using the event target to figure out which child was interacted with. It scales better than adding a listener to every child element.
Q: Do frameworks like React replace the DOM? A: No. Frameworks give you a friendlier way to describe UI, but under the hood they still update the same DOM. Understanding the DOM makes those frameworks far easier to learn and debug.
Related Reading
Get The Print Version
Download a PDF of this article for offline reading.
About the Publisher
SkillVeris Team
Engineering Team
Our engineering writers turn abstract code concepts into hands-on, project-driven learning experiences.
View all postsRelated Posts
Never miss an update
Get the latest tutorials and guides delivered to your inbox.
No spam. Unsubscribe anytime.