I'm trying to get values from the options of my select-element. It is possible to select any option, and each option features some custom data-* attribute like e.g. ...
<option value="1" data-tokens="1" data-price="500">bbbb</option>
I need to access the value of the data-price attribute which I try as follows ...
let elm = document.getElementById('treatmentCSelect');
elm.addEventListener('change', function (evt) {
let selectedOption = this.options[this.selectedIndex];
let price = 0;
price = sumar(price, parseInt(selectedOption.getAttribute('data-price'), 10));
console.log(selectedOption);
// console.log(parseInt(selectedOption.getAttribute('data-price'), 10));
// console.log(price);
});
But it always does refer just the first option element ...
<option value="1" data-tokens="1" data-price="500">bbbb</option>
My question is, how does one compute the total amount of all selected options' custom data-price-attribute values?
The problem is that you're trying to retrieve the data-price attribute from the element itself, rather than from the selected element. Try below approach