Lecture 13: DOM Manipulation and Event Handling¶
Every interactive web page you have ever used — one that updates without a full reload, reacts to clicks, or validates a form as you type — does it through the DOM. This lecture shows you how JavaScript reads, changes, and reacts to the structure of a web page.
In This Lecture¶
- What the DOM tree, nodes, and the
documentobject actually are - Selecting elements with
getElementById,querySelector, andquerySelectorAll - Creating, updating, and removing elements
- Working with
classListand inline styles - Handling events with
addEventListenerand the event object - The event life cycle — capturing, target, and bubbling — plus delegation,
preventDefault, andstopPropagation - Unobtrusive JavaScript, and why
addEventListener("DOMContentLoaded", ...)is usually a better choice thanwindow.onload
The DOM Tree, Nodes, and the document Object¶
The DOM (Document Object Model) is a live, in-memory, tree-shaped representation of an HTML page that the browser builds after parsing your HTML. Every tag becomes an object — called a node — that JavaScript can read and modify. When you change the DOM, the browser immediately redraws the page to match.
A few important vocabulary words:
- Node: any single item in the tree — an element, a piece of text, or a comment.
- Element: a node that represents an HTML tag, like
<p>or<div>. document: a special global object that represents the entire page, and is your entry point for interacting with the DOM (document.getElementById(...),document.title, etc.).- Parent / child / sibling:
bodyis the parent ofh1andp;h1andpare siblings (children of the same parent).
The DOM is not the same as your HTML file
The DOM is what the browser builds from your HTML — and JavaScript can change it after the fact. If you use "View Page Source" you see the original HTML file, but if you use "Inspect Element" (DevTools) you see the current DOM, which may look very different after scripts have run.
Selecting Elements¶
Before you can change something on the page, you need to select the node(s) you want.
// By id — returns a single element, or null if not found
const title = document.getElementById("main-title");
// By CSS selector — returns the FIRST matching element, or null
const firstButton = document.querySelector(".btn");
const firstInput = document.querySelector("input[type='email']");
// By CSS selector — returns ALL matching elements as a NodeList
const allButtons = document.querySelectorAll(".btn");
querySelector and querySelectorAll accept any valid CSS selector — by class (.btn), by
tag (div), by attribute ([type="text"]), or combinations (ul li.active) — which makes them
far more flexible than the older getElementById/getElementsByClassName methods.
querySelectorAll returns a NodeList, not an Array
A NodeList supports forEach, but not map or filter directly. Convert it first if you
need those: Array.from(allButtons) or [...allButtons].
Creating, Updating, and Removing Nodes¶
Creating and Inserting¶
const newItem = document.createElement("li"); // create a new <li> element
newItem.textContent = "New task"; // give it text content
const list = document.querySelector("#task-list");
list.appendChild(newItem); // add as the last child
list.prepend(newItem); // add as the first child
list.insertBefore(newItem, list.children[1]); // insert at a specific position
Updating Content and Attributes¶
const heading = document.querySelector("h1");
heading.textContent = "Updated Title"; // sets plain text (safe — treats input as text)
heading.innerHTML = "<em>Updated</em> Title"; // parses the string AS HTML
const link = document.querySelector("a");
link.setAttribute("href", "https://example.com");
console.log(link.getAttribute("href"));
link.removeAttribute("target");
innerHTML and security
innerHTML parses whatever string you give it as real HTML. If that string ever comes from
user input (a comment box, a search field) and you insert it with innerHTML, a malicious
user could inject a <script> tag — this is called XSS (Cross-Site Scripting), and you
will study it later in this course. Prefer textContent unless you specifically need to
insert HTML markup, and always trust textContent over innerHTML for anything user-typed.
Removing Nodes¶
const item = document.querySelector("#task-3");
item.remove(); // modern, simplest way
// Older way, still seen in existing code:
item.parentNode.removeChild(item);
classList and Inline Styles¶
Rather than changing CSS with raw strings, use classList to toggle predefined CSS classes — this
keeps styling logic in your CSS file, where it belongs.
const box = document.querySelector(".box");
box.classList.add("highlighted");
box.classList.remove("hidden");
box.classList.toggle("active"); // adds it if missing, removes it if present
console.log(box.classList.contains("active")); // true or false
You can also set individual CSS properties directly through the style property, which writes
inline styles (CSS applied directly on the element, with the highest priority):
Prefer classList over style
Toggling a class is usually better than setting individual style properties in JavaScript, because it keeps the "what it should look like" (CSS) separate from "when it should look that way" (JavaScript), and it's easier to change the look later without touching your script.
Events and addEventListener¶
An event is something that happens in the browser that your code can respond to — a click, a key press, a page load, a form submission, and many more. An event listener is a function you register to run when a specific event happens on a specific element.
const button = document.querySelector("#save-btn");
button.addEventListener("click", function () {
console.log("Button was clicked!");
});
// Arrow function version
button.addEventListener("click", () => console.log("Clicked!"));
addEventListener(eventType, handlerFunction) is preferred over the older onclick = ... style
because it lets you attach multiple listeners to the same event on the same element, and you
can remove a specific one later with removeEventListener.
function handleClick() {
console.log("Handled once");
}
button.addEventListener("click", handleClick);
button.removeEventListener("click", handleClick); // must pass the SAME function reference
The Event Object¶
Every event handler automatically receives an event object describing what happened:
document.querySelector("input").addEventListener("keydown", (event) => {
console.log(event.key); // which key was pressed, e.g. "Enter"
console.log(event.target); // the exact element the event happened on
console.log(event.type); // "keydown"
});
document.querySelector("form").addEventListener("submit", (event) => {
event.preventDefault(); // stop the default action (page reload) — see below
console.log("Form data captured without reloading the page");
});
Common events you will use constantly: click, submit, input, change, keydown,
keyup, mouseover, mouseout, load, DOMContentLoaded.
The Event Life Cycle: Capturing, Target, and Bubbling¶
When an event fires on an element nested inside other elements, it doesn't just affect that one element — it travels through the entire DOM tree in three ordered phases. Together these are called the event's life cycle:
- Capturing phase: the event starts at
documentand travels down the tree, through every ancestor of the element it's heading for, without yet having reached it. - Target phase: the event arrives at the exact element the user interacted with — the target. Both capturing-phase and bubbling-phase listeners registered directly on the target fire here.
- Bubbling phase: the event then travels back up the tree, through the same ancestors,
in reverse order, all the way back to
document.
By default, addEventListener only reacts during the target and bubbling phases. You can
make it react during the capturing phase instead by passing true (or
{ capture: true }) as a third argument:
element.addEventListener("click", handler, true); // capturing phase
element.addEventListener("click", handler, false); // bubbling phase (default)
Watching All Three Phases in Order¶
The life cycle is easiest to believe once you log it. Given three nested elements —
<div id="outer"><div id="middle"><button id="inner">Click</button></div></div> — attach both a
capturing and a bubbling listener to every level, then click the innermost button:
["outer", "middle", "inner"].forEach((id) => {
const el = document.getElementById(id);
el.addEventListener("click", () => console.log(`CAPTURE: ${id}`), true);
el.addEventListener("click", () => console.log(`BUBBLE: ${id}`), false);
});
Clicking the button logs, in this exact order:
Notice inner (the target) logs both of its own listeners back-to-back — that's the target
phase, sitting right in the middle between capturing going down and bubbling coming back up. You
can check which phase a handler is currently running in from the event object itself:
inner.addEventListener("click", (event) => {
console.log(event.eventPhase); // 1 = capturing, 2 = at target, 3 = bubbling
console.log(event.target); // always #inner — where the click actually happened
console.log(event.currentTarget); // whichever element THIS listener is attached to
});
Why capturing exists at all
Almost all real code only ever uses the bubbling phase — it's what event delegation (below) relies on. Capturing listeners are rare in application code, but they matter for one specific job: intercepting an event before anything closer to the target gets a chance to react to it or stop it, which is occasionally useful for logging, analytics, or a parent that needs the first word on every click inside it, no matter what a child's own handler does.
Event Delegation¶
Because events bubble up, you can attach one listener on a parent element instead of separate listeners on every child — this is called event delegation. It's especially useful for lists where items are added or removed dynamically, since a listener on individual items would need to be re-attached every time.
const list = document.querySelector("#task-list");
// One listener on the parent handles clicks on ANY current or future <li>
list.addEventListener("click", (event) => {
if (event.target.tagName === "LI") {
event.target.classList.toggle("done");
}
});
Here, event.target is the specific element that was actually clicked (a particular <li>),
while event.currentTarget is the element the listener is attached to (list itself).
Why event delegation matters
If you add 100 new <li> items after the page loads, a delegated listener on the parent
<ul> automatically handles clicks on all of them — no need to attach 100 separate listeners
or re-attach one every time an item is added.
preventDefault and stopPropagation¶
These two methods on the event object control the event's behavior, but they do very different things:
event.preventDefault(): stops the browser's default behavior for that event — like a form actually submitting and reloading the page, or a link actually navigating.event.stopPropagation(): stops the event from continuing to bubble (or capture) to other elements — but the default browser behavior still happens unless you also callpreventDefault().
document.querySelector("a.disabled-link").addEventListener("click", (event) => {
event.preventDefault(); // the link will NOT navigate anywhere
});
document.querySelector(".dropdown-toggle").addEventListener("click", (event) => {
event.stopPropagation(); // clicking this button won't also trigger a listener
// on a parent element (e.g. a "click outside to close" handler)
});
Don't confuse the two
preventDefault() cancels what the browser would have done. stopPropagation() cancels
the event from reaching other listeners further up (or down) the tree. A single handler can
call both when needed, but they solve different problems.
Unobtrusive JavaScript¶
Every earlier tutorial in this course bound events with an HTML attribute — onclick="save()"
sitting right inside the tag. That's a fine way to learn, but it has real costs once a page grows:
the behavior is scattered through the HTML instead of living in one place, the same attribute can
only hold one handler, and some security policies (CSP) reject inline attribute handlers
outright.
Unobtrusive JavaScript is the practice of keeping all behavior out of the HTML entirely —
no onclick, no onsubmit, nothing. The HTML describes structure only; a separate .js file
selects its own elements and attaches its own listeners with addEventListener.
// script.js — all behavior lives here instead
document.getElementById("save-btn").addEventListener("click", save);
This separation is why addEventListener is the technique real production code reaches for, even
though attribute binding is easier for a first project: the HTML stays reusable and readable on
its own, and a single element can respond to the same event with more than one independent
listener — try that with an onclick attribute and the second one you write simply overwrites
the first.
The Problem: Running Script Before the Page Is Ready¶
An unobtrusive script has to solve a timing problem attribute binding never had. onclick="save()"
works no matter when it runs, because the browser doesn't even look at it until the button exists
and is clicked. But a <script> that calls document.getElementById("save-btn") before that
button has been parsed gets null back, and .addEventListener on null throws an error and
stops your whole script.
<head>
<script src="script.js"></script> <!-- runs immediately, before <body> exists yet -->
</head>
<body>
<button id="save-btn">Save</button>
</body>
JavaScript needs to wait until the elements it wants to select actually exist. There are two standard ways to wait, and they wait for different things.
window.onload — Waits for Everything¶
window.onload = function () {
// Runs only after the HTML, CSS, images, and every other linked resource
// has fully finished downloading.
document.getElementById("save-btn").addEventListener("click", save);
};
window.onload fires only once every resource the page references — every <img>, every
stylesheet, every embedded video — has completely finished loading. That makes it reliable, but
slow: a page with one large, slow-loading image delays onload by exactly as long as that image
takes, even if your script has nothing to do with images at all.
DOMContentLoaded — Waits Only for the HTML¶
document.addEventListener("DOMContentLoaded", function () {
// Runs as soon as the HTML has been fully parsed into the DOM —
// it does NOT wait for images, fonts, or other external resources.
document.getElementById("save-btn").addEventListener("click", save);
});
DOMContentLoaded fires as soon as the DOM tree itself is built — every element exists and can be
selected — without waiting for images or other slow external resources to finish downloading.
Since almost every unobtrusive script only needs the elements to exist (not the images inside
them), DOMContentLoaded fires sooner and is the right choice for the vast majority of
"wait until the page is ready" code.
How to choose
Use addEventListener("DOMContentLoaded", ...) for almost everything — selecting elements,
attaching listeners, populating initial content. Reach for window.onload only when your code
genuinely needs a fully loaded resource first — for example, reading img.naturalWidth from an
<img>, which isn't accurate until the image data has actually arrived.
window.onload = fn only allows ONE handler
Assigning directly to window.onload (or any element.onclick, element.onsubmit, etc.)
silently replaces any handler already assigned there — it's really no different from the
onclick="..." attribute in that respect. addEventListener("DOMContentLoaded", fn), by
contrast, can be called many times, and every listener still runs. This is one more reason
addEventListener is the preferred, fully unobtrusive tool: window.onload is really a
leftover from before addEventListener existed.
A script placed at the very end of <body> (after all the HTML above it) typically doesn't need
either one — by the time the browser reaches that <script> tag, everything before it is already
parsed. DOMContentLoaded becomes essential specifically when your script is loaded from <head>,
or anywhere else that might run before the elements it needs actually exist.
Try It Yourself¶
- Build a simple to-do list: an
<input>, an "Add"<button>, and an empty<ul id="list">. When the button is clicked, create a new<li>with the input's text and append it to the list, then clear the input. Useevent.preventDefault()if you wrap the input in a<form>. - Add one delegated
clicklistener on#listthat toggles adoneCSS class (with a line-through style) when any<li>is clicked, and removes the<li>entirely if the user clicks a small "×" you add inside each item — usingevent.targetto tell which part was clicked. - Build three nested
<div>s and attach both a capturing and a bubbling listener to each, like the example above. Log each firing to the page (not just the console) and confirm you see all six lines in the exact capture-then-target-then-bubble order described above. - Convert that same page to fully unobtrusive JavaScript: remove every
onclickattribute, move the script tag to<head>, and wrap all of yourdocument.getElementById/addEventListenersetup code in a singledocument.addEventListener("DOMContentLoaded", ...)call so it still works even though the script now runs before the body exists.
Key Takeaways¶
- The DOM is a live, tree-shaped, in-memory representation of the page;
documentis your entry point into it. getElementByIdselects by id;querySelector/querySelectorAllselect by any CSS selector and are more flexible.- Prefer
textContentoverinnerHTMLunless you specifically need to insert HTML, to avoid XSS security risks with user-supplied text. classList.add/remove/toggle/containsis the preferred way to change an element's appearance; reserve directstylechanges for one-off, dynamic values.addEventListenerregisters event handlers and supports multiple listeners per event; every handler receives an event object describing what happened.- Every event travels through three ordered phases — capturing (down from
document), target (at the element itself), and bubbling (back up todocument) — andaddEventListener's third argument chooses which of the first and last you listen on. - Bubbling is what makes event delegation possible: one listener on a parent handles events from many (even future) children, instead of attaching — and re-attaching — a separate listener to every single one.
preventDefault()stops the browser's default action;stopPropagation()stops the event from reaching other listeners — they are not the same thing.- Unobtrusive JavaScript keeps all behavior in
.jsfiles and out of HTML attributes, usingaddEventListenerexclusively — this allows multiple independent listeners per element and keeps structure and behavior separately maintainable. - Wrap DOM setup code in
document.addEventListener("DOMContentLoaded", ...)so it runs as soon as the HTML is parsed, without waiting on slow-loading images the waywindow.onloaddoes; reach forwindow.onloadonly when you specifically need a resource to have finished loading.