Injecting new HTML removes previously attached listeners [duplicate]












1
















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");
});
})();









share|improve this 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.























    1
















    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");
    });
    })();









    share|improve this 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.





















      1












      1








      1









      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");
      });
      })();









      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.


























          3 Answers
          3






          active

          oldest

          votes


















          2














          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.






          share|improve this answer































            0














            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");
            });
            })();





            share|improve this answer































              0














              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 direct innerHTML manipulation.







              (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");
              });
              })();








              share|improve this answer
































                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                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.






                share|improve this answer




























                  2














                  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.






                  share|improve this answer


























                    2












                    2








                    2







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 10:14









                    Alexey S. KiselevAlexey S. Kiselev

                    1046




                    1046

























                        0














                        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");
                        });
                        })();





                        share|improve this answer




























                          0














                          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");
                          });
                          })();





                          share|improve this answer


























                            0












                            0








                            0







                            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");
                            });
                            })();





                            share|improve this answer













                            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");
                            });
                            })();






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 10:17









                            TagasTagas

                            571516




                            571516























                                0














                                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 direct innerHTML manipulation.







                                (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");
                                });
                                })();








                                share|improve this answer






























                                  0














                                  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 direct innerHTML manipulation.







                                  (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");
                                  });
                                  })();








                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    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 direct innerHTML manipulation.







                                    (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");
                                    });
                                    })();








                                    share|improve this answer















                                    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 direct innerHTML manipulation.







                                    (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");
                                    });
                                    })();






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 24 '18 at 10:33

























                                    answered Nov 24 '18 at 10:14









                                    MamunMamun

                                    27.8k71830




                                    27.8k71830















                                        Popular posts from this blog

                                        Tonle Sap (See)

                                        I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                                        Guatemaltekische Davis-Cup-Mannschaft