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

112
Views
How to check whether all the checkboxes are checked in Reactjs?

I am new to react and I got a scenario where I have multiple checkboxes on my form. I want the user to check all the checkboxes only then enable the submit button on the form. On Load, the submit button will be disabled.
How to do this?

Here is the code that I have written so far:

const MultipleCheckboxes = () => {
  const handleChange = () => {

  }

  const allChecked = () => {

  }

  return (
    <div>
      <form>
        <div className="form-check">
          <input
            type="checkbox"
            className="some-class-name-chk"
            name="someName"
            value="Java"
            id="languageChkDefault"
            onChange={handleChange}
          />
          <label
            className="form-check-label"
            htmlFor="languageChkDefault"
          >
            Javascript
          </label>
        </div>
        <div className="form-check">
          <input
            type="checkbox"
            className="some-class-name-chk"
            name="someName"
            value="Angular"
            id="languageChkDefault"
            onChange={handleChange}
          />
          <label
            className="form-check-label"
            htmlFor="languageChkDefault"
          >
            Angular
          </label>
        </div>
        <div className="form-check">
          <input
            type="checkbox"
            className="some-class-name-chk"
            name="someName"
            value="Python"
            id="languageChkDefault"
            onChange={handleChange}
          />
          <label
            className="form-check-label"
            htmlFor="languageChkDefault"
          >
            Python
          </label>
        </div> <br />
        <button disabled={!allChecked}>Submit</button>
      </form>
    </div>
  );
};

export default MultipleCheckboxes;  

Can anybody help me on this? thank you

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use react useState hook and set its default value to false.

const [state, setstate] = useState(false)

When the user clicks on an input box set the state to true. You may encounter some problems when the user unchecks the box, so you can use an if statement to handle state change For example:

if (state === true){
setstate(false)
}else{
setstate(true)
}

or you can just use this code inside the handleChange function:

!state

It inverses the state to true/false accordingly.

After that you can use a ternary operator inside the component to check whether the user has checked all the boxes, if the user hasn't checked all the boxes, disable the button or else do otherwise. For example, if the state is true (or in other words, if the user has checked the box), render the normal styled button, else render the disabled button:

{state? <button className = "styled"/>: <button disabled className="styled"/>}

Of course, I have only checked the state of one input box. Now you can simply check for multiple conditions by declaring multiple states for each box.

{state1 && state2 && state3 ? <button className = "styled"/>: <button disabled className="styled"/>}

If you are not yet familiar with ternary operators, you should go through this doc Ternary operators. If you haven't heard of useState and react hooks yet, feel free to look the React's documentation. Welcome to the React ecosystem!

about 4 years ago · Juan Pablo Isaza Report

0

This is my solution, I hope it helps you

import { useState, useEffect } from "react";

const MultipleCheckboxes = () => {
  const [allChecked, setAllChecked] = useState(false);
  const [checkboxes, setCheckboxes] = useState({
    javaCheckbox: false,
    angularCheckbox: false,
    pythonCheckbox: false
  });

  function handleChange(e) {
    setCheckboxes({
      ...checkboxes,
      [e.target.id]: e.target.checked
    })
  }

  useEffect(() => {
    const result = Object.values(checkboxes).every(v => v);

    console.log(result);

    setAllChecked(result);
  }, [checkboxes]);

  return (
    <div>
      <form>
        <div className="form-check">
          <input
            type="checkbox"
            className="some-class-name-chk"
            name="someName"
            value={checkboxes.javaCheckbox}
            id="javaCheckbox"
            onChange={handleChange}
          />
          <label
            className="form-check-label"
            htmlFor="languageChkDefault"
          >
            Javascript
          </label>
        </div>
        <div className="form-check">
          <input
            type="checkbox"
            className="some-class-name-chk"
            name="someName"
            value={checkboxes.angularCheckbox}
            id="angularCheckbox"
            onChange={handleChange}
          />
          <label
            className="form-check-label"
            htmlFor="languageChkDefault"
          >
            Angular
          </label>
        </div>
        <div className="form-check">
          <input
            type="checkbox"
            className="some-class-name-chk"
            name="someName"
            value={checkboxes.pythonCheckbox}
            id="pythonCheckbox"
            onChange={handleChange}
          />
          <label
            className="form-check-label"
            htmlFor="languageChkDefault"
          >
            Python
          </label>
        </div> <br />
        <button disabled={!allChecked}>Submit</button>
      </form>
    </div>
  );
};

export default MultipleCheckboxes; 

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!