Answers for "using data selectors in onchange event (passing option attribute to select onchange)"

0

using data selectors in onchange event (passing option attribute to select onchange)

function myFunction(sel) {
  var opt = sel.options[sel.selectedIndex];
  var price = opt.dataset.price
  document.getElementById("demo").innerHTML = "You selected: " + price;
}

<p>Select a new car from the list.</p>

<select id="mySelect" onchange="myFunction(this)">
  <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>
Posted by: Guest on October-21-2021

Code answers related to "using data selectors in onchange event (passing option attribute to select onchange)"

Code answers related to "Javascript"

Browse Popular Code Answers by Language