In my code, I have a h1 container with h1 element inside like below :
Title H1
Instead of h1.addEventListener(...)
add
document.body.addEventListener('click', function(element) {
if (element.target.nodeName == 'H1') {
// your code
}
})
So you bind event listener to body
rather than h1
.
The thing is that your h1.addEventListener(...)
is only applied on currently in DOM existing elements, but not on dynamically created ones.
http://www.theappguruz.com/blog/dynamic-event-binding-demo-jquery
In jQuery, how to attach events to dynamic html elements?