1

How to get the value of checkbox in react js , it is always returning false (boolean value) this is my snippet code

export default class CreateMateriel extends Component {
    constructor(props) {
        super(props);
        this.onChangeEtat = this.onChangeEtat.bind(this);


  this.state = {
        etat: false

onChangeEtat(e) {
    this.setState({
        etat: e.target.value
    });

the html code part

                    <input type="checkbox" className="form-control" value={this.state.etat}
                           onChange={!this.onChangeEtat}/>

Please do anyone has an idea?

3
  • onChange={this.onChangeEtat} and onChangeEtat(e) { this.setState({ etat: e.target.checked }); Commented Apr 24, 2021 at 12:15
  • Thanks @ShivamJha onChangeEtat(e) { this.setState({ etat: e.target.checked }); console.log(e.target.checked) } Commented Apr 24, 2021 at 12:21
  • 1
    Put it in a reply so I make it the correct answer Commented Apr 24, 2021 at 12:21

1 Answer 1

1

you can acess whether checkbox is checkd using e.target.checked:

onChangeEtat(e) {
    this.setState({
        etat: e.target.checked
    });
}
Sign up to request clarification or add additional context in comments.

Comments

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.