I ran into something weird playing with events on Javascript, I tried adding events to an element, and modifying the parent element inner HTML deleted the events, here is an example :
const content = document.body
const p = document.createElement('p')
p.innerHTML = "I am just a text element."
p.addEventListener("click", function(event){
console.log("The event works..")
})
content.appendChild(p)
content.innerHTML += "<div> </div>"
Now, clicking on the "p" element should trigger the event but the last line "content.innerHTML..." prevents it. It somehow delete the event.
I know modifying the inner HTML like this is not a good practice, but still, can someone explain why the events are deleted ?
Thank you !