Adding an HTML element to the body or a parent div using the += operator clears the event listeners.
The following is the code I wrote to investigate this issue. The overall result is losing the event listeners when adding the last HTML element. However, it is interesting that when adding an HTML element to the body, it clears all previous event listeners, but when we add an HTML element to the div (id = parent), it clears the event listeners of the siblings but not the body.
How can we append HTML to a specific element without unpredictably losing event listeners anywhere?
document.getElementById('button1').addEventListener('click', e => {
document.getElementById('message').innerHTML = `<h3>You clicked button 1</h3>`
})
document.getElementById('button2').addEventListener('click', e => {
document.getElementById('message').innerHTML = `<h3>You clicked button 2</h3>`
})
document.getElementById('addToBody').addEventListener('click', e => {
document.body.innerHTML += '<button id="button3">No. 3 Element in Body</button>'
document.getElementById('button3').addEventListener('click', e => {
document.getElementById('message').innerHTML = `<h3>You clicked button 3</h3>`
})
document.getElementById('message').innerHTML = `<h3>You added an element to the body</h3>`
})
document.getElementById('addToParent').addEventListener('click', e => {
document.getElementById('parent').innerHTML += '<button id="button4">No. 4 Element in Parent Div</button>'
document.getElementById('button4').addEventListener('click', e => {
document.getElementById('message').innerHTML = `<h3>You clicked button 4 Parent Div</h3>`
})
document.getElementById('message').innerHTML = `<h3>You added an element to the Parent</h3>`
})
document.getElementById('addToBody2').addEventListener('click', e => {
document.body.innerHTML += '<button id="button5">No. 4 Element in Parent Div</button>'
document.getElementById('button5').addEventListener('click', e => {
document.getElementById('message').innerHTML = `<h3>You clicked button 5 </h3>`
})
document.getElementById('message').innerHTML = `<h3>You added a 2nd element to the body</h3>`
})
<body>
<div id="parent">
<button id="button1">No. 1</button>
<button id="button2">No. 2</button>
</div>
<button id="addToBody">Add an Element to the Body</button>
<button id="addToParent">Add a Sibbling to the Parent Div with ID of parent</button>
<button id="addToBody2">Add a 2nd Element to the Body</button>
<div id="message"></div>
</body>
New Element
overrides whatever in the element, but element.innerHTML += "Element to append
does not override. However, it messes with the existing event listeners. – EricandWeb Oct 27 '22 at 20:46