DOM & Browser APIs

Events

Events are how the browser tells your JavaScript that something happened — a click, a keypress, a form submission, or a network response. Mastering the event model is essential for building interactive UIs.

How do you handle events in JavaScript?

Attach a function with element.addEventListener("click", handler). The handler receives an event object with details such as event.target (what was clicked) and event.key (which key was pressed). Call event.preventDefault() to stop default behaviour like a form submitting or a link navigating.

const button = document.querySelector("#save");
button.addEventListener("click", (event) => {
  console.log("clicked", event.target);
});

form.addEventListener("submit", (e) => {
  e.preventDefault();
  save(new FormData(form));
});

input.addEventListener("input", (e) => console.log(e.target.value));

Event Propagation Phases

1. Capture phase

Event travels DOWN from window → document → html → ... → target. Listeners with { capture: true }.

2. Target phase

Event is at the target element. Both capture and bubble listeners fire here.

3. Bubble phase

Event travels UP from target → ... → html → document → window. Default behavior.

Key Event Methods

e.preventDefault()Prevent default browser action (link navigation, form submit…)
e.stopPropagation()Stop event from bubbling/capturing further
e.stopImmediatePropagation()Also stop other listeners on the same element
e.targetThe original element that triggered the event
e.currentTargetThe element the listener is attached to

Essential Event Types

Mouse

  • click
  • dblclick
  • mousedown/up
  • mousemove
  • mouseenter/leave
  • contextmenu

Keyboard

  • keydown
  • keyup
  • key property (e.g. Enter)
  • code property (e.g. KeyA)
  • Modifier keys: ctrlKey, shiftKey
  • e.preventDefault() for shortcuts

Form & Focus

  • input (every keystroke)
  • change (on commit/blur)
  • submit (form)
  • focus / blur
  • focusin/out (bubbling)
  • reset

Document

  • DOMContentLoaded
  • load (full page)
  • beforeunload / unload
  • visibilitychange
  • resize
  • scroll

Pointer

  • pointerdown/up/move
  • pointerenter/leave
  • pointercancel
  • gotpointercapture
  • Unifies mouse + touch + stylus
  • Check e.pointerType

Custom

  • new CustomEvent('name', {detail})
  • el.dispatchEvent(event)
  • bubbles: true/false
  • cancelable: true/false
  • e.detail for payload
  • Event bus pattern

Common Pitfalls

Using onclick = fn (property assignment) — replaces existing handlers
Use addEventListener — allows multiple handlers on the same element
Memory leaks — adding listeners without ever removing them
Remove listeners when component unmounts / element is removed using removeEventListener with the same reference
Arrow functions in removeEventListener — different references each time
Store handler in a variable to pass the same reference to both add and remove
Forgetting passive: true on scroll/touch listeners — jank on mobile
Add { passive: true } to scroll and touchstart listeners that don't call preventDefault

Interactive Events Demo

Code Editor

<button id="btn">Click</button>
<input id="field" placeholder="Type, then press Enter">
<a id="link" href="https://example.com">Link (navigation prevented)</a>
<pre id="out"></pre>

Preview

Browser events — step by step

1 / 4
1

Event propagation — capture, target, and bubble

When you click a nested element, the event travels in three phases. Capture (top-down): from window down to the target. Target: the element itself receives the event. Bubble (bottom-up): event propagates back up to window. By default, addEventListener listens in the bubble phase. Pass true as the third argument to listen in the capture phase.

1 of 4
💡Use once option for one-time listeners
Pass { once: true } to addEventListener to automatically remove the listener after it fires once. This is cleaner than manually calling removeEventListener and prevents memory leaks from forgotten listeners on elements that get removed from the DOM.

🎯 Quick Check

What does event.stopPropagation() do?

JavaScript Events: summary

Key points

  • Common events: click, input, change, submit, keydown, focus, blur, scroll, DOMContentLoaded.
  • Events bubble up from the target to its ancestors; stopPropagation() stops that.
  • Remove listeners with removeEventListener (same function reference) or an AbortController signal.
  • Use { passive: true } for scroll and touch listeners so they don't delay scrolling.

Frequently asked questions

›What is the difference between the input and change events?

input fires on every keystroke or value change. change fires when the user commits the change, such as on blur for text fields or on selection for checkboxes and selects.

›Should I use onclick or addEventListener?

addEventListener. It allows several handlers, options like once and passive, and keeps JavaScript out of your HTML.