event delegation

Handling events for many child elements with one listener on a shared ancestor, using event.target to find which child was involved.

In more detail

Instead of hundreds of listeners, a single one on the list checks event.target.closest('button.del') and reads the row's data attribute. Items added later work automatically, and removed items leave no listeners behind. Use focusin and focusout, not focus and blur, since only they bubble.

Example

React attaches one listener per event type to each root container; since React 17 not to the document, so several React apps can share a page.

Related terms

Taught in

References