OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Intersection Observer using querySelectorAll throws a TypeError

  • Thread starter Thread starter Sackadelic
  • Start date Start date
S

Sackadelic

Guest
I'm getting stuck on a Intersection Observer problem. Using document.querySelectorAll('.entry') throws a TypeError: "Uncaught TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element'."

Changing document.querySelectorAll('.entry'); to document.querySelector('.entry'); solves the issue and it works.

What am I doing wrong?

The basic HTML structure:

Code:
<html>
<body>
  <div id="content-container">
    <div class="content">
      <div class="entry">Some content here on each entry</div>
      <div class="entry"></div>
      <div class="entry"></div>
      <div class="entry"></div>
      <div class="entry"></div>
      <div class="entry"></div>
    </div>
</div>
</body
</html>

The JavaScript:

Code:
const blog = document.querySelectorAll('.entry');
const options = {
  root: null,
  rootMargin: '0px',
};
const observer = new IntersectionObserver(function(entries, observer) {
  entries.forEach((entry) => {
    if (!entry.isIntersecting) {
      return;
    } 
    else {
      console.log(entry);
    }
  });
}, options);
observer.observe(blog);

<p>I'm getting stuck on a Intersection Observer problem. Using <code>document.querySelectorAll('.entry')</code> throws a TypeError: "Uncaught TypeError: Failed to execute 'observe' on 'IntersectionObserver': parameter 1 is not of type 'Element'."</p>
<p>Changing <code>document.querySelectorAll('.entry');</code> to <code>document.querySelector('.entry');</code> solves the issue and it works.</p>
<p>What am I doing wrong?</p>
<p><strong>The basic HTML structure:</strong></p>
<pre><code><html>
<body>
<div id="content-container">
<div class="content">
<div class="entry">Some content here on each entry</div>
<div class="entry"></div>
<div class="entry"></div>
<div class="entry"></div>
<div class="entry"></div>
<div class="entry"></div>
</div>
</div>
</body
</html>
</code></pre>
<p><strong>The JavaScript:</strong></p>
<pre><code>const blog = document.querySelectorAll('.entry');
const options = {
root: null,
rootMargin: '0px',
};
const observer = new IntersectionObserver(function(entries, observer) {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
return;
}
else {
console.log(entry);
}
});
}, options);
observer.observe(blog);
</code></pre>
 

Latest posts

S
Replies
0
Views
1
Safwan Aipuram
S
Top