0

I use this snippet to manipulate the action when clicking the 'a' tag within all '.read-more' div's:

[].forEach.call(document.querySelectorAll(".read-more > a"),function(el){el.addEventListener("click",function(e){...}

It works perfectly, except when I use infinite scrolling. All '.read-more a' elements that is not loaded at page loading, but when infinite scrolling is loading them, does not get affected by my snippet.

How to make the snippet affect the elements that is loaded by infinite scroll and not just the visible ones at page load?

  • 1
    Does this answer your question? [Attach event to dynamic elements in javascript](https://stackoverflow.com/questions/34896106/attach-event-to-dynamic-elements-in-javascript) – Robin Zigmond Jan 20 '23 at 22:54
  • 1
    Or just use [event delegation](https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation). (Also your snippet is calling Array.prototype.forEach on a NodeList which implements its own forEach method.) – pilchard Jan 20 '23 at 23:00

0 Answers0