1

React Checkbox not getting value onCheck . I tried an accepted answer, and get constant value first , but it is getting undefined(data variable)

<TableCell>
    <Checkbox name="myCheckBox1" onChange={(e, data) => this.checkchange1('2', data.value)}/>
</TableCell>


checkchange1 = (e, data) => {
        console.log('it works');
        console.log(data.value);
    }

what am I missing ?

2
  • in checkchange1, data.value returns undefined? If yes, calling this.checkchange1('2', data) should console data.value in checkchange1 function. Check Commented Nov 7, 2019 at 10:33
  • If data.value does not exist. We can pick value from e as well in checkchange1 by e.target.value Commented Nov 7, 2019 at 10:38

2 Answers 2

1

To get the value of checkbox, Try like below

<Checkbox name="myCheckBox1" value="checkedA" onChange={(e) => this.checkchange1(e)}/>
checkchange1 = (e) => {
    console.log(e.target.value); /* checkedA will be consoled here */
}
Sign up to request clarification or add additional context in comments.

Comments

0

onChange will take only one param and that is e.

you can store the value in state like this.

<Checkbox onChange={this.toggle} checked={this.state.checked} /> 

 toggle = () => this.setState(({checked}) => ({ checked: !checked }))

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.