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

213
Views
Why is my function only used on one element?

so i was trying to build an inner HTML counter to a specific number wich gets activated when it is in viewport. My code works fine but only on the last of the 3 elements in my ".countarea" Section. When i use console.log it shows all of my 3 divs. Why is only working on the last div?

//Counter Animation
let counter = document.querySelectorAll(".counter");
console.log(counter);
counter.forEach((counter) => {
    counter.innerText="0";
    updatecounter = () => {
        let i = +counter.innerText;
        let target = +counter.getAttribute("data-target");
        let increcement = target / 15000;

        if (i < target) {
            counter.innerText = Math.ceil(i + increcement);
            setTimeout(updatecounter);
        }  else {
            counter.innerText = target;
        }
    }
}); 

let countarea = document.querySelector("section.countarea");

function isInViewport () {
    if (
        countarea.getBoundingClientRect().top < this.window.innerHeight &&
        countarea.getBoundingClientRect().bottom > this.window.innerHeight
        ) {
        updatecounter();
        } else {console.log("false")}
};

window.addEventListener("scroll", isInViewport) ;
about 4 years ago · Juan Pablo Isaza
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!