Reputation: 573
I have two text fields id and name of the user . Below that a submit button . How can I pass the user name and Id to handleSubmit() on click of the submit buton ?
<input type="text" name="name" id="name" />
<br/>
<label>id : </label>
<input type="text" name="userId" id="userId" />
<br />
<input type="submit" oncClick={()=>this.handleSubmit()}value="Add
user"/>
Upvotes: 0
Views: 213
Reputation: 330
The value should be saved in the state of the component and updated onChange
. Once handleSubmit
is called you read the value from the state. const { name } = this.state
class form extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit() {
console.log(this.state.value)
}
render() {
.....
}
}
Upvotes: 1