admin管理员组文章数量:1426182
I have the following button with event attached:
<button className="pull-right btn btn-success" onClick={this.onNextStep}>Next</button>
OnNextStep:
onNextStep: function (e) {
...
}
How do I get the button name inside onNextStep?
e.target.value is not working
And how do I change it?
I have the following button with event attached:
<button className="pull-right btn btn-success" onClick={this.onNextStep}>Next</button>
OnNextStep:
onNextStep: function (e) {
...
}
How do I get the button name inside onNextStep?
e.target.value is not working
And how do I change it?
Share Improve this question edited Feb 29, 2016 at 21:08 Evan Davis 36.7k7 gold badges52 silver badges58 bronze badges asked Feb 29, 2016 at 20:47 Rares MardareRares Mardare 2731 gold badge5 silver badges15 bronze badges 3-
e.innerHTML
should work. A button doesn't have a value. – Matthew Darnell Commented Feb 29, 2016 at 20:51 - @MatthewDarnell Are you sure? MDN (and functioning code) seem to think otherwise, e.g., developer.mozilla/en-US/docs/Web/HTML/Element/…, stackoverflow./q/5701831/438992) This button doesn't, but I'm pretty sure they can. – Dave Newton Commented Feb 29, 2016 at 21:03
- I realize OP is using React, but this question has nothing to do with React. – Evan Davis Commented Feb 29, 2016 at 21:08
4 Answers
Reset to default 5e.target.textContent
will give you the text of the button
I would suggest something like this:
<button
type="button"
className="pull-right btn btn-success"
onClick={() => this.onNextStep('Next')}
>
Next
</button>
Remember that React is mostly about writing to the DOM. Rarely do you read from it. (The exception being inputs.)
Your "value" is just a static piece of text. There is no need to get what you've already got.
You need a name attribute on that button name="next step"
and you'll get it via e.target.name
. To change it you could assign name={this.state.buttonName}
and set state on that key when you need to (this.setState({ buttonName: "new button name" });
). If you are talking about inner HTML as in the ment by Matthew, your button's name could be called from state <button>{this.state.name}</button>
and setState could change that as well.
in react you can use onClick{(e) => e.target.innerHTML }
本文标签: javascriptget clicked button39s nameStack Overflow
版权声明:本文标题:javascript - get clicked button's name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745398344a2656920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论