javascriptintersection-observer

Check if an element is being observed, IntersectionObserver


Is there any way to determine if an element is being observed already?

if ( element /* is not being observed */ ) observer.observe(element)

Solution

  • There does not currently seem to be a way to detect if an element is being observed. To over come this the simplest solution is to add a flag to an element when you attach the Observer to it. Then you can look for this flag in the future when you need to check if it has already been added.

    element.dataset.libraryNameObserverType = true