Skip to content
Browser & DOMChapter 38 / 47Beginner4 min read

DOM Events

Listen to click, input and keydown events with addEventListener in JavaScript. Learn the event object, preventDefault, once and removing listeners with AbortSignal.

On this page

1. What are DOM events?

When the user clicks, types, scrolls, or the page loads, the browser fires an event each time. With addEventListener you attach a "listening" function to an element.

When the event happens, the browser passes your function an event object with all the details: which element it happened on, which key was pressed, where the mouse was.

2. Why does it matter?

An interactive page means responding to what the user does. Without events, JavaScript has no idea what the user did.

Old inline onclickaddEventListener
HTML and JavaScript mixed togetherJavaScript stays clean in a separate file
Only one handler per elementMany listeners on one event
No control over optionsonce, passive and signal options
Hard to removeremoveEventListener or abort

3. Where is it used?

EventUse case
clickButtons, menus, cards
inputLive search, character counter
changeSelect, checkbox, file input
submitSending a form
keydownShortcuts, closing a modal with Escape
DOMContentLoadedSetup once the DOM is ready

4. How does it work?

From click to handler
Register a listener with addEventListener
The user clicks the button
The browser creates an event object
The listener runs from the task queue
The handler works with the event object
Event object propertyMeaning
event.targetThe element the event actually happened on
event.currentTargetThe element the listener is attached to
event.keyWhich key was pressed, such as "Enter"
event.preventDefault()Stop the browser's default action
event.stopPropagation()Stop the event from travelling up to parents

5. Code example

Click and input

js
const counterBtn = document.querySelector("#counter");
const search = document.querySelector("#search");
let count = 0;

counterBtn.addEventListener("click", () => {
  count++;
  counterBtn.textContent = `Clicked ${count}`;
});

search.addEventListener("input", (event) => {
  console.log("Search:", event.target.value);
});

Keyboard, preventDefault and once

js
document.addEventListener("keydown", (event) => {
  if (event.key === "Escape") closeModal();
  if (event.ctrlKey && event.key === "s") {
    event.preventDefault();
    saveDraft();
  }
});

welcomeBtn.addEventListener("click", showTour, { once: true });

Removing all listeners with AbortController

js
const controller = new AbortController();
const { signal } = controller;

window.addEventListener("resize", onResize, { signal });
window.addEventListener("scroll", onScroll, { signal, passive: true });

controller.abort();

How does the code work?

LineWhat happens
"click" listenerThe count goes up on every click
event.target.valueThe input's current text
event.preventDefault()The browser's "Save page" dialog does not open
{ once: true }The listener removes itself after the first run
{ signal }Listeners are removed on controller.abort()
passive: trueTells the browser the scroll will not be blocked

Interview tip

event.target vs event.currentTarget is a common question. If you click a span inside a button, target is the span, but currentTarget is the element the listener is attached to.

6. Common mistakes

Mistake: Calling the handler instead of passing it

Why? In addEventListener('click', save()), save runs immediately, not on click.

Better: Pass a reference: addEventListener('click', save).

Mistake: Removing an anonymous function with removeEventListener

Why? Every arrow function is a new function, so the reference does not match and the listener is not removed.

Better: Keep a reference to a named function, or use AbortController with the signal option.

Mistake: Adding a separate listener to every item

Why? A hundred items means a hundred listeners, and items added later get no listener at all.

Better: Add one listener on the parent, which is event delegation.

Mistake: Calling stopPropagation everywhere

Why? The event never reaches other listeners, such as analytics or dropdown-closing code.

Better: Use it only when needed. Checking event.target is often enough.

7. What to learn before and after

Suggested order
DOM Basics
DOM Events
Event Delegation
Forms and Validation

Final quick revision

PointDetail
addEventListener(type, fn, options)Attach a listener
Event objecttarget, currentTarget, key
preventDefault()Stop the default browser behaviour
onceRuns one time, then removes itself
signalRemove listeners with AbortController
passiveSmoother performance for scroll and touch