Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 27 / 31THE BROWSER

An interaction travels.

Events identify what happened and move through the document tree.

01 / THE BASICS

Start here.

An event is a report that something happened: a click, a key press, a form submission, or a change in input. An event listener is a function registered to run when a specified event reaches an element. addEventListener('click', handler) registers the handler; it does not call it immediately. The browser later supplies an event object with information such as the target that began the interaction.

Events can travel through the DOM tree. During bubbling, an event that starts on a button can also reach its parent section and higher ancestors. A listener on the parent can therefore respond to clicks on children, which is called event delegation. event.target names where the event began; event.currentTarget names the element whose listener is currently running. They can differ, and choosing the wrong one can make code act on an unexpected node.

Some browser actions have defaults. A link click navigates, and a form submission normally sends data or reloads the page. event.preventDefault() asks the browser not to perform that default for a cancelable event. event.stopPropagation() stops further travel through the tree, which is a different choice. Use these only when the interface supplies another clear behavior. The event model makes interactions predictable when you trace the starting node and the path upward.

02 / THE IDEA

Build the model.

An event listener reacts to an event rather than repeatedly checking for input. Browser events move through capture and bubble phases. A listener on a parent can often handle events from children because the event bubbles upward.

The event's target is where it began; currentTarget is the element whose listener is running. This difference matters in delegated handlers. preventDefault stops a browser default action when that event is cancelable; stopPropagation changes event travel. Use each only when the interaction needs it.

03 / UNDER THE SURFACE

How it works.

addEventListener registers a function for a named event. When a click occurs, the browser creates an event object and dispatches it through the document tree. The event may travel down during capture, reach its target, then bubble through ancestors. A parent listener can inspect event.target to learn which child started the interaction.

A delegated listener is useful for a list whose buttons may be added later. event.currentTarget is the element with the running listener; event.target is the origin. If target is a nested span inside a button, closest('button') walks upward to find the button. In production code, ensure the target is an Element before calling closest, and check that the found button belongs to the intended list.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const list = document.querySelector('ul');
list?.addEventListener('click', event => {
  if (!(event.target instanceof Element)) return;
  const button = event.target.closest('button');
  if (button && list.contains(button)) console.log(button.dataset.id);
});

First notice. One parent listener can serve buttons added later.

If the page has a ul, querySelector returns it and the optional chain registers one click listener. A click on a button inside the list bubbles to that listener. The instanceof check ensures target is an element; closest finds its enclosing button. contains prevents a button outside this list from being used. The console then displays the button's data-id value. A button added to the list later uses the same listener.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const list = document.querySelector('ul');
list?.addEventListener('click', event => {
  console.log(event.currentTarget.tagName, event.target.tagName);
});

Clicking a button inside the list would print UL and BUTTON, assuming the button itself was the target. currentTarget stays the ul while that listener runs; target follows where the event began. Clicking a span inside the button would make the second value SPAN.

06 / KEEP THIS

One thing to remember.

Read target and currentTarget as two different locations in an event's journey.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.