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

131
Vistas
How to change value of all checkboxes in react?

I want to change value of my checkboxes from true to false

 var x = document.getElementsByClassName("checkbox")
      for(let i=0; i<=x.length; i++) {
         x[i].checked = false;
       } 

but when I try to do this I get an error: Cannot set properties of undefined (setting 'checked') because x is HTMLCollection and x[i] returns only
<input type="checkbox" class="checkbox"> so no 'checked' property.
How can I handle this?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Please either change the <= to < in the for loop

for(let i=0; i<x.length; i++) {}

Or make it to loop till length - 1

for(let i=0; i<=x.length - 1; i++) {}
about 4 years ago · Juan Pablo Isaza Denunciar

0

The issue with how you're doing it is that you're not making use of the states in React.

This is how I would do it.

Set initial state of the checkboxes:

constructor(props) {
    super(props);
    this.state = {checked : false};
    }

then pass the state into the checked property of the input:

<input type="checkbox" checked={this.state.checked} onChange={(e) => this.handleChange(e)} />

In the handleChange function update the state of the checkbox:

handleChange = (e) => {
    this.setState({ checked: e.target.checked })
  }

This should update all the checkboxes that have checked={this.state.checked}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this:

<input type="checkbox" checked={this.state.chkbox} onChange={this.handleChangeChk} />

Or you can check this question here briefly explained:

Set checkbox value in React JS

about 4 years ago · Juan Pablo Isaza Denunciar
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