0

I'm new to reactjs. I have created below code to make checkbox and to get value of checkbox. But on unchecking the checkbox I'm still getting the value. How to remove value of unchecking the checkbox. Please help.

below is in html

<input onChange={(event)=>this.getCheckboxValue(event)} type='checkbox' value={item.title}/>

Below is function

 getCheckboxValue(event) {
   console.log(event.target.value)
 }
1
  • but also I need to get the value Commented Sep 8, 2019 at 18:35

1 Answer 1

3

The value of the checkbox doesn't change. You're interested in whether it's checked (event.target.checked) in addition to its value (event.target.value).

<input onChange={this.onChange} type='checkbox' value={item.title}/>
onChange = (event) => {
  const {checked, value} = event.target;
  if (checked) {
    // do something with value
  }
}

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

4 Comments

Yes..got it..but also I need to get the value of checked element
@SagarKodte what do you mean get the value? Can you be more specific?
there is value={item.title} I need to get this when checked and if unchecked it should be blank
I have done this.setState({checked:value}) console.log(this.state.checked) this.but on first checked m getting empty

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.