Skip to main content

Command Palette

Search for a command to run...

JavaScript Event Delegation Explained

Updated
JavaScript Event Delegation Explained
V

I'm a solutions engineer lead, GitHub Star, Director of WomenDevsSG, and co founder of ragTech. I work at the intersection of tech, systems, and leadership, and this blog is where I share my journey through all three. Expect honest reflections, real experiences, and thoughts that are still forming rather than polished career advice.

Hello coders! Today, I’ll be explaining a useful and powerful event handling concept in JavaScript called Event Delegation. But first, a little background knowledge is needed.

Background Knowledge: Event Capturing & Bubbling

An ‘onclick’ attribute is usually attached to a <button> element in HTML. This attribute allows the button to run an event (function) when someone clicks on it. Similar to when you add an addEventListener() to a button from a script.

For ‘onclick’ event handlers, 2 phases occur: Capturing and Bubbling

Event Capturing: browser checks from the topmost element <html> for an onclick handler on it. If it does, it will run it before moving down to the next HTML element and repeat the process until it reaches the target (clicked) element. AKA Top to Target.

On the other hand,

Event Bubbling: browser checks from the target (clicked) element for an onclick handler on it. If it does, it will run it before moving up to the next HTML element and repeat the process until it reaches the topmost element <html>. AKA Target to Top.

Here’s the visual explanation of the 2 phases

Note: In modern browsers, by default, all event handlers are registered for the bubbling phase. Source: developer.mozilla.org/en-US/docs/Learn/Java..

For this reason, using too much ‘onclick’ handlers can sometimes get messy, especially when you have an event that runs in an element with a parent element who has an ‘onlick’ attribute too. The parent element’s ‘onclick’ will be triggered even if you don’t want it to.

A solution to this issue is:

  1. Use addEventListener() instead

  2. Add event.stopPropagation() in the event function

Though it might seem that these 2 phases are troublesome, they are not entirely bad. In fact, as a developer, Event Capturing and Bubbling can be used to implement a more efficient way to code event handlers. Yes, this efficient event handling concept is known as Event Delegation.

Event Delegation

A powerful and efficient event handling pattern to use when a lot of elements in a parent will run the same event.

Let’s take a look at an example. Say you have a “to-do list” app like this:

Instead of adding an event handler in each “COMPLETED” button to remove the task when the button is clicked, we can just place one event handler in the parent element.

Here’s the HTML:

<div id="task-box">
    <h2>My Tasks</h2>
    <div id="task">
        <button class="done">COMPLETED</button>
        <p>9am-10am</p>
        <p>Walk my dog</p>
    </div>
    <div id="task">
        <button class="done">COMPLETED</button>
        <p>10am-12pm</p>
        <p>Do homework</p>
    </div><div id="task">
        <button class="done">COMPLETED</button>
        <p>12pm-4pm</p>
        <p>Lunch with friends</p>
    </div>
</div>

So instead of an ‘onclick’ to each <button> element. We have an 'onclick' assigned to <div id="task-box"> element that will check if button was clicked and runs a removeTask() function to delete that button and its task from the to-do list. Let's code that:

document.getElementById("task-box").onclick = function(event) { 
       if (event.target && event.target.matches("button.done")) {
                  removeTask(event.target.parentNode); 
       } 
});

And the result worked perfectly:

Explanation

When the button is clicked, the Bubbling phase checks if the button (target element) has an ‘onclick’. It does not, so it moves up to <div id = “task”> and checks again. No it doesn’t have an ‘onclick’ too, so it moves up again to <div id=”task-box”>. Aha! An ‘onclick’ is found! So it will run the function, which checks the event.target (the clicked element). If it is a button with class = “done”, then we shall remove the <div id = “task”> (parentNode of event.target) by calling the function removeTask().

And that’s it!

I hope this simple example helps you visualize how Event Delegation works and how it uses Event Bubbling/Capturing to implement its concept. It is very helpful in situations like this to-do list, when elements are constantly being removed and added. It will be a hassle to add an ‘onclick’ to each button.

There are so many wonderful uses to implementing Event Delegation. I hope you can apply it to your projects. Thank you for reading and if you find it helpful, please leave a ‘thumbs up’ to let me know~ If you have any questions regarding Event Capturing/Bubbling and Event Delegation, please do not hesitate to ask in the comments. Till next time, cheers!

S

As the unofficial faculty historians, the varsity yearbook employees should cowl the events and moments of every year Emeril Lagasse French Door Air Fryer 360 home Best Air Fryer Grill Combo In 2022 Air Fryer Lif For Instant Pot 8 Qt In 2022 and tell the school’s story. Given the importance of every yearbook, the employees and consultants should beware to confirm the standard of the book.

W
werom19515y ago

Articles by Victoria has a blog of JavaScript event delegation explained. You can get zipjob help to learn more new ways of education easily. They are providing the information of bubbling and capturing with online assessment. It includes an HTML element and repeats the process until it reaches the target element. Join it for more.

Super Newbie Web Dev

Part 42 of 49

Let's be honest. We all started from level 0. There's no shame in that. Here's the series for the newbies~ Hope you can learn and feel free to ask a lot of questions! Check my other series: Random project to build

Up next

Beginner's Guide to Troubleshooting in JavaScript

Types of Errors If you are new to JavaScript, you probably encounter the 2 most common type of errors: Syntax Errors Logic Errors Syntax errors are basically spelling errors in your code. JavaScript is case-sensitive! So a little mistake in the ...