Home > Enterprise >  How to check whether all the checkboxes are checked in Reactjs?
How to check whether all the checkboxes are checked in Reactjs?

Time:05-23

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

CodePudding user response:

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; 

CodePudding user response:

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!

  • Related