Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target value sometimes undefined #4745

Closed
mnlfischer opened this issue Aug 31, 2015 · 10 comments
Closed

Target value sometimes undefined #4745

mnlfischer opened this issue Aug 31, 2015 · 10 comments

Comments

@mnlfischer
Copy link

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.

@sophiebits
Copy link
Collaborator

Can you post a small reproducible example in https://react.jsbin.com/ or jsfiddle? I can't help you unless you give more information.

@mnlfischer
Copy link
Author

https://jsbin.com/zekixo/
click the plus button and check console output.

@jimfb jimfb reopened this Sep 1, 2015
@jimfb
Copy link
Contributor

jimfb commented Sep 1, 2015

Bug occurs when the click target is the glyph instead of the button. Original browser mouse event has target correctly set, synthetic event does not have the target set. Reproducible in both 0.13 and 0.14.

@sophiebits
Copy link
Collaborator

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.

@sophiebits
Copy link
Collaborator

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.

@horaciogithub
Copy link

Oh my God, I love you sophiebits, I have been fighting against this issue for weeks and did not find any solution, thanks a lot!! :)

@srinivas-goli
Copy link

@sophiebits Thansk a lot!! it's working ..

@gejimayu
Copy link

@sophiebits saves my life, thanks

@konstantinkrumin
Copy link

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.

Ohh thank you so much, @sophiebits ! I was quite confused when my console was randomly giving 'undefined' when buttons clicked

@Aqsa48
Copy link

Aqsa48 commented Jul 25, 2021

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.

Thank you it also worked for me with the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants