DOM Manipulation & Events
Selecting and changing page content with JS, and the event model that lets your code react to what the user does.
DOM Manipulation & Events
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The DOM is a live, in-memory tree of objects — querySelector and querySelectorAll walk that tree using CSS selectors to find the nodes you want to change.
- classList, textContent, and node creation methods (createElement, append, remove) let you change what's on screen without ever touching a string of HTML.
- Events don't just fire on the element you clicked — they bubble upward through every ancestor, which is what makes event delegation possible.
- addEventListener's third argument controls whether a handler runs during the capture phase or the bubble phase, and most of the time you want the default: bubbling.
- Event delegation — one listener on a parent, reading event.target — is cheaper and more reliable than attaching a listener to every child element, especially when children are added later.
The page is a tree you can edit live
When a browser parses HTML, it doesn’t keep the HTML around — it builds a tree of objects, one per element, called the DOM (Document Object Model). That tree is what actually gets painted to the screen, and crucially, it’s mutable while the page is open. This is the whole premise of interactive web pages: JavaScript doesn’t rewrite the HTML source, it reaches into that live tree and changes objects directly, and the browser repaints whatever changed.
That reframes the problem of “making a button do something” into two separate problems: finding the right node in the tree, and changing it in a way the browser will reflect on screen.
Finding nodes: querySelector and querySelectorAll
document.querySelector('.card.active') returns the first element matching that CSS selector; document.querySelectorAll('.card') returns all of them, as a static list. The reason these take CSS selector strings rather than, say, a direct reference is that you usually don’t have a reference yet — you’re describing a pattern (“the first element with this class”, “all list items inside this container”) and asking the DOM to search its tree for matches. If you already know an element has an id, document.getElementById('nav') does the same job slightly faster, but querySelector is the one tool that generalizes to anything you can express as CSS.
Once you have a node, you can read and write its properties: el.textContent for its text, el.classList.add('hidden') / .remove('hidden') / .toggle('hidden') for its classes, and el.style.color for inline styles. classList is worth calling out specifically — it exists so you don’t have to hand-parse and reassemble a class attribute string yourself; it gives you set-like operations on the element’s classes instead.
Building and removing nodes
Sometimes you don’t want to change an existing node, you want to add or remove one entirely — rendering a new comment, clearing a list before re-rendering it. document.createElement('li') makes a node that exists only in memory; it has no effect on the page until you attach it somewhere with parentNode.append(newNode) (or appendChild, its older sibling). Going the other direction, node.remove() detaches a node from the tree, and the browser stops rendering it immediately.
A common mistake here is building up HTML as a string and dropping it in with innerHTML. It works, but it forces the browser to re-parse that string into nodes from scratch every time, and it’s a genuine security risk if any part of that string came from user input — a <script> tag in Database-sourced text, inserted via innerHTML, will run. Building nodes with createElement and setting textContent (never innerHTML) for user-supplied text sidesteps that entirely.
Reacting to the user: addEventListener
Selecting and editing nodes handles rendering. The other half of an interactive page is responding to what the user does, and that’s what addEventListener is for: button.addEventListener('click', handler) runs handler every time that button is clicked. You can listen for dozens of event types this way — 'input' for typing, 'submit' for forms, 'mouseover', 'keydown', and more.
Bubbling, capturing, and why it matters
Here’s the part that trips people up: an event doesn’t just fire on the element it happened to. The browser computes a path from the document root down to the clicked element — this is the capture phase — and then fires the event back up from that element to the root — the bubble phase. By default, addEventListener listens during the bubble phase, which is why clicking a button inside a div inside a body triggers, in order: the button’s listener, then the div’s, then the body’s. Pass { capture: true } as a third argument and a listener runs on the way down instead, before the target’s own handler — rarely needed, but it explains why some libraries’ “global click to close a dropdown” logic works at all.
Event delegation: using bubbling on purpose
Bubbling isn’t just a quirk to be aware of — it’s a tool. Say you have a <ul> with a hundred <li> items, and clicking any of them should do something. Attaching a hundred listeners is wasteful, and worse, it breaks the moment you add item 101 dynamically, because that new node never got a listener. Instead, attach one listener to the <ul> itself. Because clicks on any <li> bubble up to it, that single listener fires regardless of which item was clicked — and event.target tells you exactly which element started the event, so you can branch on it (event.target.dataset.id, for instance). This pattern, called event delegation, is the standard way production code handles lists, tables, and any container whose children change over time. It’s cheaper, and it’s correct by construction for content that doesn’t exist yet when the page first loads.
Check yourself
Why does querySelector take a CSS selector string instead of, say, an element's variable name?
Because the DOM is a tree you don't already hold references into — a CSS selector describes a pattern to search for, which is far more flexible than requiring you to already have a handle on the element.
If you click a button inside a div, and both the button and the div have click listeners, what order do they fire in?
The button's listener fires first, then the div's — because the event starts at the target and bubbles outward through its ancestors, unless a listener was registered for the capture phase instead.
Why would you attach one click listener to a <ul> instead of one to every <li> inside it?
Because a single listener on the parent catches clicks on every child via bubbling, including children added to the list after the page loaded — whereas per-child listeners have to be reattached every time the list changes.
What does event.target tell you that document.querySelector can't?
event.target is the actual element the user interacted with, which you only know at the moment the event fires — querySelector would have to guess in advance which of potentially many matching elements was clicked.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.