MEPX
Chapter 14 of 16All chapters

Chapter 14 of 16

Events

Listening for what the visitor does.

Listeners

addEventListener attaches a function to an event on an element. The same element can have many listeners, and removeEventListener needs the same function reference to detach one.

  • The event object carries target, key, coordinates and preventDefault.
  • Events bubble up the tree, so a listener on a container can serve every child.

Delegation

Listening on a parent and checking event.target.closest is called delegation. It survives content being replaced, which is why it suits anything rendered dynamically.