Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 274
oldboy Avatar asked Mar 10 '26 03:03

oldboy


1 Answers

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
like image 156
David Bradshaw Avatar answered Mar 11 '26 15:03

David Bradshaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!