I'm trying to retrieve a custom data attribute that I did add to the option element, but it's not working.
<p>Select a new car from the list.</p>
<select id="mySelect" onchange="myFunction(this.data-price)">
<option data-price="250" value="Audi">Audi</option>
<option data-price="130" value="BMW">BMW</option>
<option data-price="120" value="Mercedes">Mercedes</option>
<option data-price="400" value="Volvo">Volvo</option>
</select>
<p>When you select a new car, a function is triggered which outputs the value of the selected car.</p>
<p id="demo"></p>
function myFunction(val) {
document.getElementById("demo").innerHTML = "You selected: " + val;
console.log(val);
}
The thing is when I switch the function call to:
onchange="myFunction(this.value)">
It returns the value correctly, but when I select the data-price, it returns undefined to the console.