DOM Events
This topic covers the moment a browser tells your code something happened. Listener options, capture and bubble phases, delegated handlers over a changing list, cleanup when a node disappears, passive scroll listeners, and visibility signals that pause work while a tab is hidden all live here. Examples name the target element they expect and show whether the handler runs during capture or bubble. Browse the task groups to separate attaching a listener from controlling one you already own, then read each explanation for the propagation and memory behaviour that determines whether the pattern survives a real page with widgets, navigation, and long-lived components that mount and unmount repeatedly during normal use.
In scope: Include examples where the central difficulty is receiving, scoping, delegating, or removing a browser event. Out of scope: Exclude layout-only or data-only examples that happen to run inside a click handler.
10 published examples across 5 primary task groups. Read the group heading to find the task you are solving, then pick the Example whose assumptions match your page.
Build a responsive layout
1 exampleArrange boxes so the design holds across viewport widths, zoom levels, and content growth.
Keep a table header visible with sticky positioning
Pin a table header to the top of its own scroll box with position: sticky on the cells, keep borders and backgrounds honest, and avoid the wrapper that silently defeats it.
Handle DOM events
3 examplesAttach, configure, delegate, and remove browser event listeners correctly.
Add an event listener with the options you actually need
Pass an options object instead of a bare boolean to addEventListener: once, capture, and passive tell the browser how the listener should behave and when it should stop mattering.
Debounce a scroll handler without dropping the last call
Wrap the scroll handler in a trailing-edge timer so a burst of scroll events collapses into exactly one call that still runs for the final position instead of dropping it.
Delegate events for a list that changes after load
Bind one click listener to a stable list container and route events with closest, so rows added or removed later need no extra wiring and cleanup never falls behind the markup.
Observe element visibility
2 examplesReact to intersection, viewport entry, or document visibility changes.
Lazy-load images with IntersectionObserver
Swap a placeholder image for the real file only when the browser is about to show it, and see where the loading attribute the platform already ships is the better answer.
Reveal an element the first time it enters the viewport
Play a reveal animation the first time an element scrolls into view, then drop its observer so the effect cannot replay when the reader scrolls back up the page.
Structure page content
3 examplesMark up document regions, reading order, and navigation so people and crawlers share one map.
Add a skip link that lands on the main region
Place a first-tab link that jumps past navigation into the main content and becomes visible on focus, so keyboard users are not trapped in the header.
Mark up a primary navigation as a labelled list
Wrap site navigation in a named nav landmark and keep its items a real list, so screen readers announce which region it is, how many links it holds, and where it ends.
Structure a page with semantic landmarks
Mark the header, navigation, main, and footer regions so keyboard and screen-reader users can jump between them instead of tabbing through everything.
Validate form input
1 exampleDecide whether entered data is acceptable and report the result to the person filling the form.
Announce field errors without moving the caret
Report validation failures beside the field they describe, announce them politely, and leave focus where the reader typed.
Adjacent navigation
- Parent: Home
- Browse by language: JavaScript
- Browse by language: CSS
- Browse by language: HTML
- Other topics: CSS Layout
- Other topics: Data Transformation
- Other topics: Fetch and HTTP
- Other topics: Form Validation
- Other topics: React State
- Or search: Search examples