Injecting new HTML removes previously attached listeners [duplicate]
This question already has an answer here:
Is it possible to append to innerHTML without destroying descendants' event listeners?
11 answers
I have some code to add some HTML and attach a listener to them, strangely when I add some more things to the page the eventlisteners for previously added elements don't work.
JS:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
javascript addeventlistener
marked as duplicate by Mohammad, Community♦ Nov 24 '18 at 12:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is it possible to append to innerHTML without destroying descendants' event listeners?
11 answers
I have some code to add some HTML and attach a listener to them, strangely when I add some more things to the page the eventlisteners for previously added elements don't work.
JS:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
javascript addeventlistener
marked as duplicate by Mohammad, Community♦ Nov 24 '18 at 12:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is it possible to append to innerHTML without destroying descendants' event listeners?
11 answers
I have some code to add some HTML and attach a listener to them, strangely when I add some more things to the page the eventlisteners for previously added elements don't work.
JS:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
javascript addeventlistener
This question already has an answer here:
Is it possible to append to innerHTML without destroying descendants' event listeners?
11 answers
I have some code to add some HTML and attach a listener to them, strangely when I add some more things to the page the eventlisteners for previously added elements don't work.
JS:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
This question already has an answer here:
Is it possible to append to innerHTML without destroying descendants' event listeners?
11 answers
javascript addeventlistener
javascript addeventlistener
asked Nov 24 '18 at 10:06
aznafroaznafro
103
103
marked as duplicate by Mohammad, Community♦ Nov 24 '18 at 12:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mohammad, Community♦ Nov 24 '18 at 12:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Modifying innerHTML will destroy and rebuild all descendant elements of the container. Event handlers bound to any of the destroyed elements are lost in the process and need to be reattached if required. Please, use .insertAdjacentHTML method.
add a comment |
When you adjust innerHTML, the whole element will be rebuilt (and any attached listeners will be lost in the process), so you have to add your listeners after the adjustments:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
add a comment |
The issue is, innerHTML rebuild the full HTML, thus all the descendant element's events are lost. You can use Element.insertAdjacentHTML() instead:
The
insertAdjacentHTML()method parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than directinnerHTMLmanipulation.
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Modifying innerHTML will destroy and rebuild all descendant elements of the container. Event handlers bound to any of the destroyed elements are lost in the process and need to be reattached if required. Please, use .insertAdjacentHTML method.
add a comment |
Modifying innerHTML will destroy and rebuild all descendant elements of the container. Event handlers bound to any of the destroyed elements are lost in the process and need to be reattached if required. Please, use .insertAdjacentHTML method.
add a comment |
Modifying innerHTML will destroy and rebuild all descendant elements of the container. Event handlers bound to any of the destroyed elements are lost in the process and need to be reattached if required. Please, use .insertAdjacentHTML method.
Modifying innerHTML will destroy and rebuild all descendant elements of the container. Event handlers bound to any of the destroyed elements are lost in the process and need to be reattached if required. Please, use .insertAdjacentHTML method.
answered Nov 24 '18 at 10:14
Alexey S. KiselevAlexey S. Kiselev
1046
1046
add a comment |
add a comment |
When you adjust innerHTML, the whole element will be rebuilt (and any attached listeners will be lost in the process), so you have to add your listeners after the adjustments:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
add a comment |
When you adjust innerHTML, the whole element will be rebuilt (and any attached listeners will be lost in the process), so you have to add your listeners after the adjustments:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
add a comment |
When you adjust innerHTML, the whole element will be rebuilt (and any attached listeners will be lost in the process), so you have to add your listeners after the adjustments:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
When you adjust innerHTML, the whole element will be rebuilt (and any attached listeners will be lost in the process), so you have to add your listeners after the adjustments:
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".container").innerHTML += "<p class="blue">BLUE</p>";
document.querySelector(".container").innerHTML += "<p class="purple">PURPLE</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();
answered Nov 24 '18 at 10:17
TagasTagas
571516
571516
add a comment |
add a comment |
The issue is, innerHTML rebuild the full HTML, thus all the descendant element's events are lost. You can use Element.insertAdjacentHTML() instead:
The
insertAdjacentHTML()method parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than directinnerHTMLmanipulation.
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();add a comment |
The issue is, innerHTML rebuild the full HTML, thus all the descendant element's events are lost. You can use Element.insertAdjacentHTML() instead:
The
insertAdjacentHTML()method parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than directinnerHTMLmanipulation.
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();add a comment |
The issue is, innerHTML rebuild the full HTML, thus all the descendant element's events are lost. You can use Element.insertAdjacentHTML() instead:
The
insertAdjacentHTML()method parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than directinnerHTMLmanipulation.
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();The issue is, innerHTML rebuild the full HTML, thus all the descendant element's events are lost. You can use Element.insertAdjacentHTML() instead:
The
insertAdjacentHTML()method parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than directinnerHTMLmanipulation.
(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();(function() {
document.body.innerHTML = "<div class="container"></div>";
document.querySelector(".container").innerHTML += "<p class="red">RED</p>";
document.querySelector(".red").addEventListener("click", function() {
console.log("red");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="purple">PURPLE</p>");
document.querySelector(".purple").addEventListener("click", function() {
console.log("purple");
});
document.querySelector(".container").insertAdjacentHTML("beforeend", "<p class="blue">BLUE</p>");
document.querySelector(".blue").addEventListener("click", function() {
console.log("blue");
});
})();edited Nov 24 '18 at 10:33
answered Nov 24 '18 at 10:14
MamunMamun
27.8k71830
27.8k71830
add a comment |
add a comment |