26

I want to retrieve the value of my checkbox when it is checked.

I am using this "http://react-component.github.io/checkbox/".

My code looks like this.

<div className="user_box">
    { check.map((values , i)=> {
        return <Checkbox
            name = "checkbox"
            onChange={this.checkvalue.bind(this)}
            value={values.username} 
        />
    })}
</div>

My function:

checkvalue(e){
    // var all_users = [];
    // var value = this.checkbox.value;
    // all_users.push(value);
    // console.log(all_users);

    console.log('checkbox checked:', (e.target.checked));
}

I'm not understanding how to retrieve the value of the checkbox.

3
  • Hey, can you make a jsfiddle/code snippet so we can help you ? :) Commented Sep 1, 2016 at 10:08
  • its difficult to explain on jsfiddle because. i cant explain, what output i want Commented Sep 1, 2016 at 10:45
  • Using the default e.target.value just getting the value on every time Commented Jan 18, 2022 at 7:06

6 Answers 6

57

you need to pass the "e, Synthetic event parameter to your handler" :

handleChange(e) {
  let isChecked = e.target.checked;
  // do whatever you want with isChecked value
}

render() {
  // ... your code here
  return (
    {/* your other jsx here */}
    <Checkbox otherProps onChange={e => this.handleChange(e)} />
    {/* your other jsx here */}
  );
}
Sign up to request clarification or add additional context in comments.

3 Comments

the same thing works when we change the onChange attribute with onChange={this.handleChange}, can you please explain why? How do they differ?
@PrameshBajracharya Because using {this.handleChange} passes a function, and onChange expects a function. I/O is compatible and hence why it works.
How are you doing this? On mine, e.target.checked is undefined
9

Use the Synthetic event parameter, here is an example:

const element1 = "boy";
const element2 = "girl";

<input
 type="checkbox"
 name={element1}
 value={element1}
 onChange={this.handleChange}
/>
<label for="element" style={{ fontSize: 35 }}>
{element2}
 </label>

<input
 type="checkbox"
 name={element2}
 value={element2}
 onChange={this.handleChange}
/>
<label for="element" style={{ fontSize: 35 }}>
{element2}
 </label>




handleChange = (e) => {
// to find out if it's checked or not; returns true or false
const checked = e.target.checked;

// to get the checked value
const checkedValue = e.target.value;

// to get the checked name
const checkedName = e.target.name;

//then you can do with the value all you want to do with it.
};

Comments

2

Try this :

getChckeboxValue(event) {
    const value = event.target.value;
}

In render:

<input onClick={this.getChckeboxValue.bind(this)} type="checkbox" value="Text" />

Comments

1

One liner:

onChange={e => doSomethingWithValue( e.currentTarget.checked )}

Comments

0

We can get check box value by declaring onChange={this.likeClicked} defaultChecked={false} on input element properties

  constructor(props){
    super(props)
    this.likeClicked = this.likeClicked.bind(this)
  }

  likeClicked(){
    console.log(event.target.checked, event.target.id);
  }

  render(){
    return (
      <div className='px-3'>
          <div className='item-header item-wrapper cirle'>
            <input 
               type="checkbox" 
               onChange={this.likeClicked}  
               defaultChecked={false}  
               className="checkbox" 
               id={`checkbox_${this.props.product.sku}`} 
            />
            <label htmlFor={`checkbox_${this.props.product.sku}`}> 
               <HeartIcon className="heart-svg"></HeartIcon>
            </label>
          </div>
      </div>
    );
  }

Comments

0

If you have to get the checkbox status value with the existing form data like input, select. here is the code

 const onChangeHandler = (e) => {
    const formFieldValue = e.target.type === 'checkbox' ? e.target.checked : e.target.value
    setFormValues({
      [e.target.name]: formFieldValue,
    })
  }

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.