javascript
32 lines · 8 steps
Event delegation with a clean teardown
A single listener on a root element handles events for many matching descendants, with an unsubscribe function for cleanup.
Explained by
highlit
1function delegate(root, eventType, selector, handler) {
2 const listener = (event) => {
3 let node = event.target;
4 while (node && node !== root) {
5 if (node.matches(selector)) {
6 handler.call(node, event, node);
7 return;
8 }
9 node = node.parentNode;
10 }
11 };
12 root.addEventListener(eventType, listener);
13 return () => root.removeEventListener(eventType, listener);
14}
15
16class EventDelegator {
17 constructor(root) {
18 this.root = root;
19 this.cleanups = [];
20 }
21
22 on(eventType, selector, handler) {
23 const off = delegate(this.root, eventType, selector, handler);
24 this.cleanups.push(off);
25 return this;
26 }
27
28 destroy() {
29 this.cleanups.forEach((off) => off());
30 this.cleanups = [];
31 }
32}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1One listener on a shared ancestor can serve many children, so you avoid attaching (and leaking) handlers on every element.
- 2Returning an unsubscribe closure makes teardown symmetric with setup and keeps the listener reference in scope.
- 3Collecting cleanup functions lets a coordinator dispose of all its listeners in one call.
Related explainers
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
javascript
import { useState, useEffect, useCallback, useRef } from "react"; export function usePersistentForm(storageKey, initialValues) { const [values, setValues] = useState(() => {
A React hook that persists form state to localStorage
custom-hooks
localstorage
debounce
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/event-delegation-with-a-clean-teardown-explained-javascript-0ad3/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.