What is Event Delegation in JavaScript?

DOM event delegation is used for responding to user events via a single parent rather than each child node. With it, you can bind an event handler to a common parent element that will handle any event happening on one of its children:

<!-- Instead of doing: -->
    <li onclick="console.log(event.type);">πŸ“•</li>
    <li onclick="console.log(event.type);">πŸ“™</li>
    <li onclick="console.log(event.type);">πŸ“—</li>

<!-- We can do: -->
<ul onclick="console.log(event.type);">
Copied to clipboard!

This has a couple of advantage over of using event listeners on individual elements:

What is Event Delegation in JavaScript?
If you would like to see more Webtips, follow @flowforfrank
Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
JavaScript Course
50 JavaScript Interview Questions


JavaScript Course Dashboard

Tired of looking for tutorials?

You are not alone. Webtips has more than 400 tutorials which would take roughly 75 hours to read.

Check out our interactive course to master JavaScript in 5 hours.

Learn More

πŸ“š Get access to exclusive content

Want to get access to exclusive content? Support webtips with the price of a coffee to get access to tips, checklists, cheatsheets, and much more. β˜•

Get access Support us
πŸŽ‰ Thank you for subscribing to our newsletter. x