How to bind dynamically generated element to Intersection Observer?

Viewed 1380

This is the code that I'm using:

var ob = new IntersectionObserver(entries => {
    for(var entry of entries){
        console.log('works');
    }
});

document.querySelectorAll('dynamicElement').forEach(elem => ob.observe(elem));

This is working for elements that are on the page, but it doesn't work for elements that get dynamically created later. How do I bind an element that's inserted in the page later ?

I know with jquery you can bind it like $(document).on('event', 'element', 'func');, but how do I do it with the observer ?

0 Answers
Related