I want to add a click event to elements with class item
. Work fine:
const enlargables = document.querySelectorAll('.item');
enlargables.forEach(function(el) {
el.addEventListener('click', function(e) {
alert('hello');
})
});
<div class="item">test 1</div>
<div class="item">test 2</div>
But if the element is added dynamically after pageload, the event will not be added to the element.
How can I add the event to newly added elements with class item
using pure JS? Similar to how document ready works in jQuery.