I have a function with jQuery that should SUM up all numbers in divs with class .priceSelectedOption (see Javascript function that add these classes to the last div of outputted values after selection dropdown), but it doesn’t show the total amount below title “Total”. Only 0.
function selectedAfternoon(element) {
var text = element.options[element.selectedIndex].value;
document.getElementById("output-selected-option-afternoon").innerHTML = "<div>" + text.split("|").join("</div><div>") + "</div>";
document.getElementById("output-selected-option-afternoon").querySelector("div:last-child").classList.add("priceSelectedOption")
}
function selectedCommute(element) {
var text = element.options[element.selectedIndex].value;
document.getElementById("output-selected-option-commute").innerHTML = "<div>" + text.split("|").join("</div><div>") + "</div>";
document.getElementById("output-selected-option-commute").querySelector("div:last-child").classList.add("priceSelectedOption")
}
$(document).ready(function(){
var total = 0;
$('.priceSelectedOption').each(function(){
total += parseFloat($(this).text().replace(/,/g,''))
})
$('.totalPrice').text(total)
})
.js-basic-single {
width: 100%;
}
#output-selections {
display: flex;
flex-direction: column;
grid-column-gap: 1rem;
grid-row-gap: 1rem;
}
.selected-option {
display: flex;
width: 100%;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.priceSelectedOption::before, .totalPrice::before {
content: '€ ';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="Namiddag" name="Namiddag" data-name="Namiddag" class="js-basic-single" onChange="selectedAfternoon(this);">
<option></option>
<option id="13x19namiddag" value="Namiddag|13x19 cm|12,50">13x19 cm, €12.50</option>
<option id="20x30namiddag" value="Namiddag|20x30 cm|22,50">20x30 cm, €22.50</option>
<option id="30x45namiddag" value="Namiddag|30x45 cm|32,50">30x45 cm, €32.50</option>
<option class="disabled" value="disabled" disabled="disabled">Wil je meer stuks of formaten van deze foto? Vermeld dit dan in de winkelwagen., </option>
</select>
<select id="Onderweg" name="Onderweg" data-name="Onderweg" class="js-basic-single" onChange="selectedCommute(this);">
<option></option>
<option id="13x19onderweg" value="Onderweg|13x19 cm|12,50">13x19 cm, €12.50</option>
<option id="20x30onderweg" value="Onderweg|20x30 cm|22,50">20x30 cm, €22.50</option>
<option id="30x45onderweg" value="Onderweg|30x45 cm|32,50">30x45 cm, €32.50</option>
<option class="disabled" value="disabled" disabled="disabled">Wil je meer stuks of formaten van deze foto? Vermeld dit dan in de winkelwagen., </option>
</select>
<div id="output-selections">
<div class="selected-option" id="output-selected-option-afternoon"></div>
<div class="selected-option" id="output-selected-option-commute"></div>
</div>
<h3>Total</h3>
<div class="totalPrice"></div>