JavaScript Event Delegation is Easier than You Think

Share this article

If you’re into adding a little JavaScript interactivity to your web pages you may have heard of JavaScript event delegation and thought it was one of those convoluted design patterns only hardcore JavaScript programmers worry about. The truth is, if you already know how to add JavaScript event handlers, it’s a snap to implement.

JavaScript events are the bedrock of all interactivity on web pages (I mean serious interactivity, not those dinky CSS drop-down menus). In traditional event handling you add or remove event handlers from each element as needed. However, event handlers can potentially cause of memory leaks and performance degradation — the more you have, the greater the risk. JavaScript event delegation is a simple technique by which you add a single event handler to a parent element in order to avoid having to add event handlers to multiple child elements.

How does it work?

Event delegation makes use of two often overlooked features of JavaScript events: event bubbling and the target element. When an event is triggered on an element, for example a mouse click on a button, the same event is also triggered on all of that element’s ancestors. This process is known as event bubbling; the event bubbles up from the originating element to the top of the DOM tree. The target element of any event is the originating element, the button in our example, and is stored in a property of the event object. Using event delegation it’s possible to add an event handler to an element, wait for an event to bubble up from a child element and easily determine from which element the event originated.

How will it help me?

Imagine an HTML table with 10 columns and 100 rows in which you want something to happen when the user clicks on a table cell. For example, I once had to make each cell of a table of that size editable when clicked. Adding event handlers to each of the 1000 cells would be a major performance problem and, potentially, a source of browser-crashing memory leaks. Instead, using event delegation, you would add only one event handler to the table element, intercept the click event and determine which cell was clicked.

What does it look like in code?

The code is simple; we only need to worry about detecting the target element. Let’s say we have a table element with the ID “report” and we have added an event handler to the table for the click event that will call the editCell function. The editCell function will need to determine the target element for the event that has bubbled up to the table. Expecting that we’ll write a few event handler functions that will need this functionality, we’ll place it in a separate function called getEventTarget:

function getEventTarget(e) {
  e = e || window.event;
  return e.target || e.srcElement;
}

The variable e represents the event object and we need only a sprinkling of cross-browser code to gain access to and return the target element, stored in the srcElement property in Internet Explorer and the target property in other browsers.

Next is the editCell function that calls the getEventTarget function. Once we have a reference to the target element it’s up to us to make sure the element is the one we’re expecting:

function editCell(e) {
  var target = getEventTarget(e);
  if(target.tagName.toLowerCase() === 'td') {
    // DO SOMETHING WITH THE CELL
  }
}

In the editCell function we confirm that the target element is a table cell by checking its tag name. That check may be over simplified; what if it’s another element inside the table cell that is the target of the event? A quick modification that adds code to find the parent td element may be needed. What if some cells should not be editable? In that case we can add a specific class name to a non-editable cell and check that the target element does not have that class name value before making it editable. Many options are available and you’ll just need to choose the one that suits your application.

What are the pros and cons?

The benefits of JavaScript event delegation are:

  • There are less event handlers to setup and reside in memory. This is the big one; better performance and less crashing.
  • There’s no need to re-attach handlers after a DOM update. If your page content is generated dynamically, via Ajax for example, you don’t need to add and remove event handlers as elements are loaded or unloaded.

The potential problems may be less clear, but once you are aware of them they’re easily avoided:

  • There’s a risk your event management code could become a performance bottleneck, so keep it as lean as possible.
  • Not all events bubble. The blur, focus, load and unload events don’t bubble like other events. The blur and focus events can actually be accessed using the capturing phase (in browsers other than IE) instead of the bubbling phase but that’s a story for another day.
  • You need caution when managing some mouse events. If your code is handling the mousemove event you are in serious risk of creating a performance bottleneck because the mousemove event is triggered so often. The mouseout event has a quirky behaviour that is difficult to manage with event delegation.

Summary

There are JavaScript event delegation examples available that use major libraries: jQuery, Prototype, and Yahoo! UI. You can also find examples using no library at all, like this one from the Usable Type blog.

Event delegation is a handy tool to have in your kit should the need arise and easy to implement.

Frequently Asked Questions about JavaScript Event Delegation

What is the main advantage of using event delegation in JavaScript?

Event delegation in JavaScript is a technique that takes advantage of the event bubbling process. The main advantage of using event delegation is that it significantly improves performance by reducing the number of event listeners attached to the elements. Instead of attaching individual event listeners to each element, you attach a single event listener to a parent element. This listener then handles events for all of its child elements. This is particularly beneficial when dealing with a large number of similar elements, such as list items or table rows.

How does event delegation work in JavaScript?

Event delegation works by taking advantage of the event bubbling process in JavaScript. When an event occurs on a child element, it bubbles up through the DOM tree, triggering the same event on each of its parent elements. By attaching an event listener to a parent element, you can handle events for all of its child elements. The event object passed to the event handler contains a ‘target’ property, which refers to the actual element that triggered the event. You can use this property to determine how to handle the event.

Can event delegation be used with any event in JavaScript?

Yes, event delegation can be used with any event in JavaScript that bubbles. However, not all events bubble. For example, the ‘focus’ and ‘blur’ events do not bubble in all browsers. For these events, you can use the ‘focusin’ and ‘focusout’ events instead, which do bubble.

How can I stop event bubbling in JavaScript?

You can stop event bubbling in JavaScript by using the ‘stopPropagation’ method of the event object. This method prevents the event from bubbling up the DOM tree. However, be careful when using this method, as it can prevent other event handlers from being triggered.

What is the difference between event delegation and event binding in JavaScript?

Event binding in JavaScript involves attaching an event listener directly to an element. This listener will only handle events for this specific element. On the other hand, event delegation involves attaching an event listener to a parent element, which handles events for all of its child elements. This technique is more efficient, especially when dealing with a large number of similar elements.

Can I use event delegation with dynamically added elements?

Yes, one of the main benefits of event delegation is that it works with dynamically added elements. Since the event listener is attached to a parent element, it can handle events for child elements that are added to the DOM after the listener is attached.

How can I use event delegation with the ‘this’ keyword in JavaScript?

When using event delegation, the ‘this’ keyword inside the event handler refers to the element to which the event listener is attached, not the element that triggered the event. To refer to the element that triggered the event, you can use the ‘target’ property of the event object.

Can I use event delegation with jQuery?

Yes, jQuery provides the ‘.on()’ method, which you can use to implement event delegation. The ‘.on()’ method takes three arguments: the event type, a selector for the child elements, and the event handler.

What is the performance impact of using event delegation?

Event delegation can significantly improve performance, especially when dealing with a large number of similar elements. By attaching a single event listener to a parent element, you reduce the number of event listeners that need to be managed by the browser, which can lead to a noticeable improvement in performance.

Can I use event delegation with custom events in JavaScript?

Yes, you can use event delegation with custom events in JavaScript. Just like built-in events, custom events can bubble up the DOM tree, allowing you to handle them with a single event listener attached to a parent element.

Andrew TetlawAndrew Tetlaw
View Author

iOS Developer, sometimes web developer and Technical Editor.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week