3
import React from 'react';
import './App.css';
import { Button } from 'react-bootstrap';

function App() {
  return (
    <div>
      <input type="checkbox" id="checkboxForHamburger" />

      <Button onClick={??????}> UNCHECK </Button>
    </div>
  );
}

export default App;

I have a Checkbox and a Button. I want the checkbox to be in "unchecked" state if the button is clicked.

What I tried-

<Button onClick={document.getElementById("checkboxForHamburger").checked = false}> UNCHECK </Button>

This is giving me an error - Cannot set property 'checked' of null

2
  • Make your checkbox controlled - reactjs.org/docs/forms.html#controlled-components. Accessing the dom directly is bad practice in react. Commented Apr 12, 2020 at 16:10
  • 2
    use state, don't go this way - it's not react Commented Apr 12, 2020 at 16:10

5 Answers 5

6

First, you checkbox isn't controlled by a state value:

Let's start by adding a state:

const [checked, setChecked] = React.useState(true);

Then we give the value to the checkbox:

<input type="checkbox" checked={checked} />

Then let's tell the button to toggle the checked state

<button onClick={() => {setChecked(old => !old)}}> {checked ? 'uncheck' : 'check'} </button>

Working example on this codesandbox

Sign up to request clarification or add additional context in comments.

Comments

2
function MyApp() {
const [checked, setChecked] = useState(false);
const handleCheck =() => {
 setChecked(!checked)
}
  return (
    <div>
      <input type="checkbox" checked={checked} />
      <Button type="button" onClick={handleCheck}> Click Me </Button>
    </div>
  );
}

export default MyApp;

Comments

0

You can do something like this-

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
        checked: true
    }
  }

  handleCheckbox = (e) => {
    e.preventDefault();
    this.setState({checked: false});
  } 

  render() {
    return (
      <div>
        <input type="checkbox" checked={this.state.checked} />
        <button type="button" onClick={this.handleCheckbox}>Uncheck</button>
      </div>
    )
  }
}

ReactDOM.render(<App />, document.querySelector("#app"));

Update

If you want to make the checkbox toggle by clicking the button then you can do this by changing the handleCheckbox function and button text.

  handleCheckbox = (e) => {
    e.preventDefault();
    this.setState({checked: !this.state.checked});
  } 

And the button would be

<button type="button" onClick={this.handleCheckbox}>{this.state.checked ? 'Uncheck': 'Check'}</button>

1 Comment

I prefer to use functional components rather than class components
0

This will work:

import React from 'react';
import ReactDOM from 'react-dom';
export default class App extends React.Component {
 constructor(){
  this.state={check:false};

   this.Toggle_checkbox=this.Toggle_checkbox.bind(this);
 }
  Toggle_checkbox=()=>{
this.setState({check:!this.state.check});
  }
 render(){
   return (
    <div>
      <input type="checkbox" checked={this.state.check} />

      <button onClick={this.Toggle_checkbox}> toggle </button>
    </div>
  );
}

 } 
ReactDOM.render(<App/>,document.getElementById('root'));

check:https://stackblitz.com/edit/react-xrt6wa

Comments

-1

I just ran this error.

Do you want to run it like the code below?

import React, { useState }  from 'react';
import './App.css';
import { Button } from 'react-bootstrap';

function App() {
const [value, setValue] = useState(false);
  return (
    <div>
      <input type="checkbox" value={value} id="checkboxForHamburger" />

      <Button onClick={() => setValue(!value)}> UNCHECK </Button>
    </div>
  );
}

export default App;

If the value is boolean, we can tell whether it is checked or not.

2 Comments

don't use state value directly.
In addition, if you want to distinguish between UNCHECK and CHECK, use the tricuspid operator to write the following together. {value ? CHECK : UNCHECK}

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.