1
    <input className="form-control"
                        type="text"
                                               
                      />
<input type="button" onClick={()=>clickHandle()}/>

When I click the button, I want to change the check property of the checkbox. But the checked property or defaultChecked properties do not work in this regard. What can I do about it?

1
  • 2
    Please add some more context to your question (the basic structure of the component). Also, there is no checkbox in the code. Commented Mar 24, 2021 at 9:22

2 Answers 2

3

As far as I understand, you want the status of the checkbox to change when the button is clicked.

If I understood correctly, this is probably what you wanted.

export default function App() {
  const [chkValue, setChkValue] = useState(false);
  return (
    <div className="App">
      <input className="form-control" type="checkbox" checked={chkValue}/>
       <input type="button" value="click" onClick={()=>setChkValue(!chkValue)} />
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oke it's worked. but it breaks the form-control
-2

You have a type text input and a type button input. There is no checkbox there. You'd need a type checkbox input and then try this answer How to set default Checked in checkbox ReactJS?

Edit:

Wrong link for setting checked value. The link explains how to set the default. This code snippet has an example of setting a checkbox value dynamically.

3 Comments

Isn't defaultChecked properties, initialize value?
@FatihBaytar you are right sorry. checked is the correct property. reactjs.org/docs/forms.html#handling-multiple-inputs this link has an example in the code with a checkbox and how to set it correctly.
Thanks. I think my main problem is with dynamically generated checkboxes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.