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.

JavascriptForm Validation

Code

snippet
// 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 level

The 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

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.