Search code examples
reactjsonchange

is it possible to get the value of input inside the onChange event


consider the following;

handleChange(e) {
    this.props.action(this.props.id, e.target.value);
  }

  render() {
    return(
      <input onChange={this.handleChange}></input>
    );
  }

is it possible to get the value of the input field inside the onChange event. Something like:

handleChange(e) {
    this.props.action(this.props.id, e.target.value);
  }

  render() {
    return(
      <input onChange={console.log(this.value),this.handleChange}></input>
    );
  }

which doesn't work


Solution

  • onChange receive event object which contains the target element, you can access the target element to get the value

    <input onChange={e => {console.log(e.target.value);this.handleChange(e) }} />