I am looking for the whole stackoverflow, but I did not get any good result regarding these problems. Change me if I'm wrong.
I want addEventListener for an object that exists or does not exist in the DOM.
In jQuery, we can simply execute the following code:
$('document').on('click', '.my-button', function () { alert("work!!!"); });
But how can I use only Javascript to get it? Below is my source code. But after the page loads, all newly added objects do not attach the click event.
var Button = document.getElementsByClassName("my-button"); for(i=0;i<Button.length;i++){ document.getElementsByClassName("my-button")[i].addEventListener("click",function(){ alert("Work!!!!"); }); }
Submit any suggestion.
source share