xijo
xijo

Reputation: 4366

React ref has no value in onChange

I wrote a react component that contains an input with a ref, but I cannot get the value of it.

Here is an example code that shows the problem, together with a jsfiddle link:

var Hello = React.createClass({
  onClick: function () {
    console.log(this.refs.name.value);
  },
  render: function() {
    return <input ref="name" onChange={this.onClick}/>;
  }
});

React.render(<Hello key="world" />, document.body);

jfiddle

Thx for all help, J

Upvotes: 1

Views: 827

Answers (1)

Wex
Wex

Reputation: 15695

Prior to React 0.14, this.refs.name is not a DOM element. To access the DOM element, you will need to call getDOMNode:

onChange: function () {
  console.log(this.refs.name.getDOMNode().value);
},

Upvotes: 1

Related Questions