Reference

HTML, CSS & browser APIs

Look up semantic HTML, forms, CSS layout, events, DOM operations and common browser APIs.

Scope and conventions

Modern browser platform examples, independent of a framework. HTML/CSS excerpts belong in the appropriate document/style context; DOM snippets assume a browser and named elements/data; TypeScript annotations clarify DOM types and need compilation or removal in plain JavaScript. Check target-browser support for newer features.

DOM & events

Handle a missing element explicitly

Web platform

querySelector returns the first match or null. TypeScript’s generic annotation describes the expected element type; it does not prove the HTML has that shape.

Fragment

Web platform · Handle a missing element explicitly
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const button = document.querySelector<HTMLButtonElement>("#save");
if (!button) throw new Error("Missing save button");
button.disabled = true;

Distinguish target from currentTarget

Web platform

target is where the event originated; currentTarget is the current listener’s element during its invocation. Nested icon/text markup can make them differ.

Fragment

Web platform · Distinguish target from currentTarget
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
button.addEventListener("click", event => {
  console.log(event.target);
  console.log(event.currentTarget);
});

Delegate events through a stable ancestor

Web platform

A parent can handle bubbling events from dynamic children. Check the target’s type and containment before using closest().

Fragment

Web platform · Delegate events through a stable ancestor
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
list.addEventListener("click", event => {
  if (!(event.target instanceof Element)) return;
  const action = event.target.closest("button[data-action]");
  if (action && list.contains(action)) handleAction(action);
});

Not every event bubbles. Shadow DOM retargeting and composed paths have additional rules.

Separate default behavior from propagation

Web platform

preventDefault requests cancellation of a cancelable default action. stopPropagation stops further propagation; it does not cancel the default action.

Fragment

Web platform · Separate default behavior from propagation
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
form.addEventListener("submit", event => {
  event.preventDefault();
  save();
});

A passive listener cannot cancel its event’s default behavior.

Remove listeners through an owned lifetime

Web platform

An AbortSignal can own several event listeners. Aborting removes those listeners; it is separate from canceling any work their handlers already started.

Fragment

Web platform · Remove listeners through an owned lifetime
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const lifetime = new AbortController();
window.addEventListener("resize", onResize, { signal: lifetime.signal });
// When the owner is disposed:
lifetime.abort();