Header Image Today (or yesterday, as of when this post will be published) I created a package to add event listeners to elements that are dynamically added to the body. So I created the dynamiceventlistener package (fancy name.. think it’ll catch on?)

It’s very simple to use. For my example page, I have it dynamically create buttons when a button is clicked, so you can see that each button has the listener registered to it when it’s dynamically added. The code is as simple as:

<button>Click Me</button>
addDynamicEventListener('button', 'click', function() {
    var button = document.createElement('button');
    button.innerHTML = 'Click Me';
    document.body.appendChild(button);
});

The selector can be anything that document.querySelector can handle, however if you use an id, class, or tag, the call is optimized.

~GitHub ~NPM ~Example

Leave feedback below, I’m interested in different ways there are to solve this problem.