5

I have complex object in React and can 't understand how to get value from checked input. There is piece of code with input. I need to add input checked value instead of words "Input value".

const ClientForm = React.createClass({

      .....

      render: function() {

          ......
          return(

              < AgForm.Input >
              < label className = 'myLabel' >
              < input className = "valueBroker"
              disabled = {
                readOnly
              }
              type = 'checkbox'
              name = 'is_broker'
              onChange = {
                this.form().checkbox('is_broker', true)
              }
              checked = {
                _isTrue(this.form().value('is_broker'))
              }
              />
              Agent < /label> < /AgForm.InputLine> < AgForm.InputLine error = {
                errors.system
              }
              /> < /div>


              module.exports = AddClientForm;



              const ClientListItem = React.createClass({

                    render() {

                      return

                      <div >
                        Input value {
                          (client.kind_name || '').split(',').map((type) => {
                            return <div > {
                              type
                            } < /div>
                          })
                        }

                      < /div>

                    })

4 Answers 4

38

I haven't used so much react so far but I think you can get the value of checkbox using :

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

2 Comments

Property 'checked' does not exist on type 'EventTarget'.
@HarshaVardhini I believe you need to bind "this" to the input... onChange={this.handleChange.bind(this)}
1

You could just bind the value and the update method to the state of your component and set the actions directly on the initial input markup with the onChange handler.

Note that React is unidirectional, not bidirectional, so it can good to try to control everything from React to your rendered html and not the other way or back and forth.

Following code is from: https://facebook.github.io/react/docs/forms.html

getInitialState: function() {
    return {value: 'Hello!'};
  },
  handleChange: function(event) {
    this.setState({value: event.target.value});
  },
  render: function() {
    return (
      <input
        type="text"
        value={this.state.value}
        onChange={this.handleChange}
      />
    );
  }

1 Comment

The question was about checkbox. Your answer is good but it does not relate to the question directly.The next answer by Himanshu is the correct one event.target.checked
0

You can use to catch the event

class MyForm extends React.Component {
    handleChange(event){
       console.info(event.target.checked,
                    event.target.value, 
                    event.target.dataset.attr);
    };

    render(){
        return (
        <input
            type="text"
            value={'myValue'}
            data-attr={'attrValue'}
            onChange={this.handleChange}
          />);
   }
};

Comments

0

If you're using React refs to retrieve your input values:

class Form extends React.Component {
    constructor(props) {
        //... state

        this.checkBoxRef = React.createRef()
    }

    handleFormSubmit = (e) => {
        e.preventDefault()
        let isChecked = this.checkBoxRef.current.checked
        console.log(isChecked) // true | false
    }

    render() {
         return (
              <Form onSubmit={this.handleFormSubmit}>
                   <input type="checkbox" ref={this.checkBoxRef}/>
                   <button type="submit" />
              </Form>
         )
    }
}

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.