1

I have written his react component

  class RegistrationSpecialNeeds extends React.Component {
     constructor(props) {
        super(props);
        this.state = {"specialneeds": []}
        this.isChecked.bind(this)
     }
     handleChange(event, key) {
        console.log('came inside handle checkbox event')
        console.log('what is event ' + event.target.checked)
        console.log('what is key' + key)
     }
     isChecked(key) {
        if (this.state.specialneeds.indexOf(key) > -1) {true} else {false}
     }
     render() {
        return (
           <div>
           <label> check all that apply
              {
                 this.props.restrictions.map((restriction, index) => 
                    <div>
                    <label>
                       <input type='checkbox' name='restriction' checked={this.isChecked(restriction.key)} onChange={(e) => this.handleChange(e, restriction.key)}/>{restriction.key}) {restriction.name}
                    </label>
                    </div>
                 )
              }
           </label>
           </div>
        )
     }
  }

<RegistrationSpecialNeeds restrictions={[{key: "a", name: "Dietary Restrictions"}, {key: 'b', name: 'Physical Disabilities'}, {key: 'c', name: 'Medical Needs'}]} />

When I select something in the checkbox I get output

[Log] came inside handle checkbox event (Registration.html, line 29) [Log] what is event undefined (Registration.html, line 30) [Log] what is keya (Registration.html, line 31)

how can I get the event object?

2 Answers 2

1
this.props.restrictions.map((restriction, index) => 
                  <div>
                  <label>
                     <input type='checkbox' name='restriction' onChange={handleChange.bind(this, restriction.key)}/>{restriction.key}) {restriction.name}
                  </label>
                  </div>
               )

try to pass the value from input field and check the value in your handleChange method. use onChange attribute

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

1 Comment

onChange(handleChange.bind(this, restriction.key)) doesn't provide the right parameters to the actual handleChange method. I did more googling and posted updated code above.
0

The following is the right way to pass the extra parameter which worked for me

onChange={(e) => this.handleChange(e, restriction.key)}

and the event handler looks like

 handleChange(event, key) {
   console.log('came inside handle checkbox event')
   console.log('what is event ' + event.target.checked)
   console.log('what is key' + key)
}

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.