1

i want to get value of first input tag in eduction [0] and value of second input tag in education[1] education is array.

<input  type="text" name="education" value={this.state.education[0]} onChange={this.handleChange}  
class="form-control"  />
<input type="text"  name="education" value={this.state.education[1]} onChange={ 
this.handleChange()} class="form-control"/>
1
  • Your second input should be onChange={this.handleChange} Commented Apr 12, 2020 at 12:45

2 Answers 2

1

It's better to do it as follow (It allow you to dynamically create your inputs and also if you don't want dynamic input you can use the same technique as well)

constructor(props) {
    super(props);
    this.state = {
      education: ["", ""] // I've added 2 items to create 2 inputs
    };
    this.handleChange = this.handleChange.bind(this);
  }

handleChange(e) {
    const education = [...this.state.education];
    education[e.target.id] = e.target.value;
    this.setState({
      education: education
    });
  }

render() {
    return (
      <div>
        {
          this.state.education.map((item, index) => (
          <input
            id={index}
            type="text"
            name="education"
            value={this.state.education[index]}
            onChange={this.handleChange}
            class="form-control"
          />
        ))
        }
      </div>
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your second input you (by mistake i think) added () after your this.handleChange function. This means this function will be called immediately and won't be called onChange.

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.