event.target Versus currentTarget
In bubbling event.target stays at the element actually hit while event.currentTarget names the element running the listener, enabling one delegated handler.
Code
// one container plus several buttons: the listener is bound to the container only
const toolbar = document.querySelector('#toolbar');
toolbar.addEventListener('click', (event) => {
// target: the element actually clicked (the button, or the parent of a text node inside it)
console.log('clicked:', event.target.tagName, event.target.dataset.action);
// currentTarget: the element currently running this listener, always toolbar
console.log('listener on:', event.currentTarget.id);
// delegation: walk up from target to the nearest button, then dispatch the action
const button = event.target.closest('button');
if (button) console.log('action:', button.dataset.action);
});
// every level the event bubbles through runs the listener: target stays fixed while currentTarget changes per levelThe file at work
Top-level bindings toolbar hold the demo state — nothing sits inside a function scope.
Points of care
- The snippet leans on
document— browser-only surfaces a bare Node process cannot provide.
Applying it
clicked:, listener on:, action: act as markers in the output; every marker maps back to one operation.
Statements that matter
const toolbar = document.querySelector('#toolbar');
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- Does event.target Versus currentTarget run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.