Target value sometimes undefined
See original GitHub issueI loop my product list with map() and create a react component for each item.
var ProductListItem = React.createClass({
handleEvent: function(e) {
console.log(e.target.value);
},
render: function() {
return (
<tr>
<th>{this.props.id}</th>
<th>{this.props.name}</th>
<th>{this.props.quantity}</th>
<th>
<button type="button" className="btn btn-default btn-sm" value={this.props.id} onClick={this.handleEvent}>
<span className="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
</th>
</tr>
);
}
});
When I click on the button sometimes the e.target.value is defined and sometimes undefined. Its totally random. After clicking on the button 50 times, the value sets magically undefined.
Issue Analytics
- State:
- Created 8 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Having an issue with e.target.value returning Undefined in React
In React, e.target.value will appear null if it's not saved in another variable.
Read more >event.target.name is undefined: What you need to know
You're likely getting this error because you're trying to get a name attribute on elements that don't have a name attribute. For example;...
Read more >undefined value from e.target.value : r/reactjs - Reddit
This way you will pass the event and be able to console log it. If you don't pass the event then it will...
Read more >undefined value from html button value attribute
I am iterating an array in template and on the button click I am trying to get the value of selected element but...
Read more >[Solved]-Difference between event.target.value and event ...
currentTarget : is the element you actually bound the event to. This will never change. Reference: Target value sometimes undefined · event target...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Wait, what?
e.target
is correctly the button or the span in my testing. If you want the button, you do definitely wante.currentTarget.value
.It looks like a bug that this is undefined, but after we fix it it’ll be the span element which probably isn’t what you want. You want
e.currentTarget.value
to get the button, which already works correctly.