0

I want make an EventListener on new querySelector added.

var ele = document.querySelector("js-new-post-activity")
ele.addEventListener('load', (event) => {
    console.log('New post added');
});

it is possible to do it with JavaScript or not? and thank you.

Klri
  • 1
  • When you say "new element add" do you mean that the element may not exist at the time the JS is run? and may be added in future? If so, look into using event delegation. – evolutionxbox Mar 11 '22 at 08:49
  • Yes it's not exist, and added when there is new post. – Klri Mar 11 '22 at 08:50
  • https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation will help you then – evolutionxbox Mar 11 '22 at 08:51
  • `document.querySelector("js-new-post-activity")` will target a `` element, I guess you wanted something like `document.querySelector(".js-new-post-activity")` which would target an element with the class `"js-new-post-activity"` – Kaiido Mar 11 '22 at 09:05

0 Answers0