DOM events
События DOM
A browser reports actions such as a button click, text input, or form submission by dispatching an event. JavaScript can listen for the event and run code in response.
Also known as: addEventListener
What a DOM event is
When you activate a button, the browser dispatches a click event. The event is an object that tells your code what happened. To react, call button.addEventListener('click', handleClick): this registers handleClick as a listener rather than calling it immediately. When the event arrives, the browser calls the listener and passes it the event object. Other event types describe different actions: input reports a user edit, while submit is dispatched on the form, not the submit button.
How an event travels
Take a button inside a panel. A click doesn't have to be handled by the button alone. In a regular DOM tree, dispatch follows this order:
- Capture: The event travels down through the ancestors toward the target. An ancestor listener registered with
{ capture: true }runs on this pass. - Target: Listeners on the target run, with capture listeners before non-capture listeners.
- Bubble: If the event bubbles, it travels back up through the ancestors, where listeners registered without
capturecan run.clickbubbles;focusdoes not. To track focus on descendants, listen forfocusinor use a capture listener forfocus.
If the button contains an icon and you click the icon, event.target may be the icon. Inside a listener registered on the panel, event.currentTarget is the panel. One tells you which node the event targeted; the other tells you where the current listener is registered.
Example: one listener for a growing list
Suppose each row in #tasks is an <li> with a button marked data-remove. Instead of wiring up every button individually, listen on the list:
const tasks = document.querySelector('#tasks');
tasks.addEventListener('click', (event) => {
const button = event.target.closest('button[data-remove]');
if (!button || !tasks.contains(button)) return;
button.closest('li').remove();
});
closest() handles clicks on content inside the button as well as on the button itself. This pattern is called event delegation. Because clicks bubble to the list, the same listener handles rows added later.
Common mistakes and interview questions
Don't confuse stopping an event with canceling the browser's default action. stopPropagation() keeps the event from traveling further but doesn't block other listeners on the current node. preventDefault() leaves propagation alone but can cancel the default action, such as following a link. It only works for cancelable events and has no effect inside a passive listener.
If you need to remove a listener, save the original function. removeEventListener() matches the event type, the function reference, and the capture flag. A new arrow function with identical code is a different function. For a listener that should run just once, use { once: true }.
A typical interview exercise asks you to predict the log order when both a child and its parent listen for a click. Mark the target first, then trace capture, target, and bubble in order. Check whether any listener stops propagation before you decide which ones run.