Skip to content
Advertisement

Why is my intersectionObserver returning my querySelect as undefined?

I am attempting to target a parent element and a child element using an intersectionObserver, then I have a function changing the background of the parent to a different color and rotating the child element.

This code works on the parent div, however the child item returns as undefined. Am I unable to target child elements with querySelector, or is the intersectionObserver unable to observe more than one element?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

You can’t observe multiple elements by passing them all to .observe, you have to call it multiple times.

Also, I assume you rather wanted to do it like this (I’m not sure if I’m right, but parts of your code didn’t make any sense to me):

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement