aBloomer aBloomer - 8 months ago 45
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"??


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: