Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
Unable to select all image elements

I'm using an intersection observer to implement lazy loading on my images. If I only select one image with the querySelector, then it works. But for some reason I can't get querySelectorAll to work. I also tried getElementsByClassName and this is still not working. Any solutions??

html:
      <section class="section" id="section--2">
          <div class="photo__container--two">
            <img
              data-src="portrait/4-vertical.JPG"
              alt=""
              class="fade-in img-vertical-lazy"
            />
            <img
              data-src="portrait/5-vertical.JPG"
              alt=""
              class="fade-in img-vertical-lazy"
            />
            <img
              data-src="portrait/6-vertical.JPG"
              alt=""
              class="fade-in img-vertical-lazy"
            />
          </div>
        </section>
    
    JavaScript:
    const lazyImages = document.querySelectorAll('.img-vertical-lazy');
    
 
    
    const lazyLoading = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (!entry.isIntersecting) return;
                entry.target.src = entry.target.getAttribute('data-src');
                lazyLoading.unobserve(entry.target)
        });
    });
    
    lazyLoading.observe(lazyImages);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

According to the InstersectionObserver docs, the observe function receives a single node, not an array of nodes

You should rewrite the last line to this


lazyImages.forEach(image => lazyLoading.observe(image))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!