JavaScript — DOM Events
Events connect users to JavaScript
An event is something that happens in the browser:
click · input · change · submit · keydown · keyup
focus · blur · mouseover · load · scroll
addEventListener — the standard pattern
const button = document.querySelector("#button");
button.addEventListener("click", () => {
console.log("Button clicked");
});
Every click re-runs the callback.
The event object
Handlers receive details about what happened:
button.addEventListener("click", event => {
console.log(event.type); // "click"
console.log(event.target); // element that originated it
});
event.target (where it came from) vs event.currentTarget (whose listener is running) differ during bubbling — crucial for delegation.
Forms — preventDefault
Submitting normally reloads the page. Intercept it:
form.addEventListener("submit", event => {
event.preventDefault();
const name = document.querySelector("#name").value;
console.log(name); // handle with JS instead
});
Input — live values
input.addEventListener("input", event => {
console.log(event.target.value); // fires on EVERY keystroke
});
Perfect for live search and inline validation. change fires only when the value is "committed" (blur/enter) — input is usually what you want for text fields.
Keyboard events
document.addEventListener("keydown", event => {
if (event.key === "Enter") console.log("Enter pressed");
if (event.key === "Escape") closeModal();
});
Mouse family
click · dblclick · mousedown · mouseup · mousemove · mouseenter · mouseleave
box.addEventListener("mouseenter", () => box.classList.add("hover"));
Named handlers & removal
function handleClick(event) { console.log("Clicked"); }
button.addEventListener("click", handleClick);
button.removeEventListener("click", handleClick); // SAME reference required
An inline arrow can't be removed — it's a different function each time.
Bubbling
Events travel from target upward through ancestors:
<div id="parent">
<button id="child">Click</button>
</div>
parent.addEventListener("click", () => console.log("parent"));
child.addEventListener("click", () => console.log("child"));
// click → "child" then "parent"
Stop the climb with event.stopPropagation() — but sparingly; delegation usually serves better.
Phases: capturing ↓ target ↓ bubbling — listeners default to bubbling; opt into capture via { capture: true }.
Event delegation — one listener, many targets
list.addEventListener("click", event => {
if (event.target.matches("button")) {
console.log("Button clicked:", event.target);
}
});
Works for buttons added later too — no re-binding. When clicks land on inner elements (e.g. a span inside the button), climb up:
const button = event.target.closest("button");
if (!button) return;
Handy listener options
el.addEventListener("click", handler, { once: true }); // auto-remove after first run
window.addEventListener("touchstart", handler, { passive: true }); // promises no preventDefault — scroll perf
const controller = new AbortController();
el.addEventListener("click", h1, { signal: controller.signal });
el.addEventListener("click", h2, { signal: controller.signal });
controller.abort(); // removes ALL of them at once
Common gotcha
button.addEventListener("click", handleClick()); // ❌ calls NOW
button.addEventListener("click", handleClick); // ✓ passes reference
Mini Practice
- Click listener logging event.target and type.
- Form submit with preventDefault reading input values.
- Live search box using the input event.
- Enter/Escape key handling on document.
- Demonstrate bubbling order parent←child; then closest()-based delegation on a list.
{ once: true }one-time button.- Remove three listeners at once via AbortController.
Next: Web APIs →
Related Topics
Frequently Asked Questions about DOM Events
What is DOM Events in JavaScript?
DOM Events is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn DOM Events?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn DOM Events.
Why is DOM Events important in JavaScript?
DOM Events is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.