question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Target value sometimes undefined

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
sophiebitscommented, Sep 9, 2015

Wait, what? e.target is correctly the button or the span in my testing. If you want the button, you do definitely want e.currentTarget.value.

8reactions
sophiebitscommented, Sep 1, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found