Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda