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

179
Vistas
JS Increment value for each onclick after checking if true

I want to check if all checboxes are checked.

For this, I got this code :

bind() {
    this.checks = document.getElementsByClassName("checkdoc");
    const len = this.checks.length;
    this.validCheckbox = 0;

    for (let i = 0; i < len; i++) {
        this.checks[i].onclick = this.updateValid;
    }

    if (this.validCheckbox === len) {
        this.allChecked = true;
        this.update();
    }
}

updateValid() {
    if (this.checked === true) {
        this.validCheckbox += 1;
    } else {
        this.validCheckbox -= 1;
    }
    console.log(this.validCheckbox);
}

But it don't work because this.validCheckbox is NaN so undefined. If I add it as parameters in this.updateValid like this :

this.updateValid(this.validCheckbox)
...

updateValid(nb) {
    if (this.checked === true) {
        nb += 1;
    } else {
        nb -= 1;
    }
    console.log(nb);
    return nb;
}

First, my console.log give me -1 on the 2nd method, then I can't do multiple calls. If I click on the 1st checkbox, nothing happens because onload, the first call is made. If you got any tips, I'll take it

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