aBloomer aBloomer - 1 year ago 66
Javascript Question

How should i get the value from custom attribute in react element?

Below is my html looks like.....

<select onclick={this.handleClick}>
<option key="1" value="aaa" data-plan-id="test"></option>

Below is my handleClick event code

console.log(e.target.value); // this will output aaa

My question is how can I get the value from "data-plan-id" attribute which is "test"??

Answer Source

Just read selectedOptions HTMLCollection and take the first option from it:


With modern browsers (IE11+, see support) you can use dataset interface instead of getAttribute:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download