jQuery to pure JS: Event listeners on dynamically created elements

The following HTML will serve as the example markup for this post. It’s an example of how you might structure a feed that receives updates without the page reloading (dynamically created content). Our goal is to add a click event listener to each new .feed-item that is dynamically created.

1<div class="feed">
2 <div class="feed-item" data-id="3">
3 <div class="item-content">
4 ...
5 </div>
7 <div class="item-controls">
8 ...
9 </div>
10 </div>
12 <div class="feed-item" data-id="2">
13 ...
14 </div>
16 <div class="feed-item" data-id="1">
17 ...
18 </div>

Event delegation

Event delegation is the process of handling events at a higher level in the DOM that where they were first triggered, thanks to behaviour known as bubbling. When an element on the page is, for example clicked, then a click event is fired on that element but it doesn’t stop there. The click event “bubbles up” the DOM tree, triggering the click events on any of the originally clicked element’s parents that are listening for one.

The jQuery approach

Using jQuery’s (1.7+) .on(), we can attach an event listener to the closest static parent of the element we want to listen for events on. (A parent element that isn’t created or destroyed dynamically.) When the static parent receives the “bubbled-up” click event, we tell it to compare the the dynamic child element’s selector passed into .on() with the element where the click originated. If they match, then the event handler is triggered. In short, this:

1$(staticParent).on(eventName, dynamicChildSelector, eventHandler)

Or, filled in to suit our needs, like this:

1$('.feed').on('click', '.feed-item', function (event) {
2 // Do something

The pure Javascript approach

There are multiple ways you can achieve this type of functionality in pure JS, depending on your needs.

The exact equivalent to our above jQuery would again leverage bubbling, a static parent and the dynamic child element’s selector, like so:

1document.querySelector(staticParent).addEventListener(eventName, function (event) {
2 if (event.target.classList.contains(dynamicChildSelector)) {
3 // Do something
4 }

Filled in to suit or needs:

1document.querySelector('.feed').addEventListener('click', function (event) {
2 if (event.target.classList.contains('feed-item')) {
3 // Do something
4 }

One of the pitfalls you may stumble into with the above approach is that not all events bubble. For example, if you want to fire an event when the mouse enters an element, you’d have to use mouseover rather than mouseenter, as mouseenter doesn’t bubble.

Alternatively, if you’re creating the element you want to listen to with document.createElement(), then as part of the creation you can pass in a value for .onclick.

1let newElement = document.createElement('div')
2newElement.onclick = // Do something

As a last resort, we could add an event listener after the new element has been created and rendered, as demonstrated in the following example. To avoid having to go through all of the previously created elements that match the selector (in our case .feed-item), it’s best to have a way to uniquely identify each one. For this example I’m using our .feed-item‘s data-id value.

1// Add the helper function for creating new event listeners
3function createEventListener (id) {
4 const element = document.querySelector(`[data-id="${ id }"]`)
6 return element.addEventListener('click', function (event) {
7 // Do something
8 })
11// Then in the function that handles rendering...
13function renderFeedItem () {
14 // Render the item
15 ...
17 // Add the event listener (where newElementsId is the id of the new element)
18 createEventListener(newElementsId)

Depending on your needs, you could make the above helper function more reusable by passing in the event and the event handler as well as the id. For real-world implementation there’s a chance you’d want rename the helper function, externalise it from the render function and fire it with a Promise instead.