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

136
Views
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 answers
Answer question

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 Report

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 Report

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 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!