I'm trying to sum up some values from multiple selectboxes. The problem is that the options contains both text and numbers. I only need the numbers and sum these up. There's no other way to do this. So what i tried was to strip text from the string and then update the amount every time the selectbox changes. Now the result is always wrong.
My other issue is that sometimes there is a minus symbol in front of the number(s). Is there any way to extract that from the total? There's an example in the third selectbox in the Fiddle.
Can anybody help with this? I've created a Fiddle here
HTML
<form method="post" id="product_configure_form" action="cart/add/14161745">
<div class="product-info-options tui">
<input type="hidden" value="" id="product_configure_bundle_id" name="bundle_id">
<div class="product-configure">
<div class="product-configure-custom">
<div class="product-configure-custom-option">
<label for="product_configure_custom_604623">Stoffering: <em>*</em>
</label>
<select id="product_configure_custom_604623" name="custom[604623]">
<option selected="selected" disabled="disabled" value="">Maak een keuze...</option>
<option value="5217777">Camira 24/7 *snellever - 2 weken</option>
<option value="5217779">Staccato *snellever - 2 weken (+€27,00)</option>
<option value="5217781">Leer 1 Trento *snellever - 2 weken (+€85,00)</option>
<option value="5217783">Leer 2 Litano (+€172,00)</option>
</select>
<div class="product-configure-clear"></div>
</div>
<div class="product-configure-custom-option">
<label for="product_configure_custom_603895">Armlegger frame kleur: <em>*</em>
</label>
<select id="product_configure_custom_603895" name="custom[603895]">
<option selected="selected" disabled="disabled" value="">Maak een keuze...</option>
<option value="5217785">zwart gecoat</option>
<option value="5217787">aluminium gecoat (+€11,00)</option>
<option value="5217789">aluminium gepolijst (+€11,00)</option>
<option value="5217791">verchroomd (+€53,00)</option>
</select>
<div class="product-configure-clear"></div>
</div>
etc............
JQUERY
function update_amounts() {
var sum = 0.0;
$('#product_configure_form .product-configure-custom-option select').each(function () {
var price = $(this).find('option:selected').text();
var priceClean = price.replace(/[^0-9]/g, '');
priceClean = parseFloat(priceClean) || 0;
sum += priceClean;
});
$('#amount').text('€' + sum.toFixed(2));
}
$( "#product_configure_form .product-configure-custom-option select" ).change(function() {
update_amounts();
});
valueattribute, and use that, not the text.