I have been struggling to update the total price for specific options. Even though the code was created by someone else and has worked fine for many users, I can't seem to make it work correctly. Any assistance would be greatly appreciated.
Here is the code I am currently using:
HTML
<form name="cart_quantity" action="" method="post">
<h4>Available Options</h4>
<p>
<input type="hidden" id="hid_id[7]" value="0"> <select id="id[7]" name="id[7]" required aria-required="true" onchange="changePrice(this.id)" class="form-control">
<option value="" selected="selected">--- Please Choose ---</option>
<option value="48">1 x nVidia RTX 2080 Ti 11GB GDDR6 blower fan design (+$1,800.00)</option>
<option value="49">2 x nVidia RTX 2080 Ti 11GB GDDR6 bblower fan design (+$3,600.00)</option>
<option value="50">3 x nVidia RTX 2080 Ti 11GB GDDR6 blower fan design (+$5,400.00)</option>
<option value="51">4 x nVidia RTX 2080 Ti 11GB GDDR6 blower fan design (+$7,200.00)</option>
<option value="27">Not needed</option>
</select>
</p>
<p>Options subtotal: <span id="productPrice"></span></p>
</form>
Javascript
var actualprice = document.getElementById('productPrice').innerHTML;
var actualInd = actualprice.indexOf("(");
var price = actualprice.substring(actualInd + 1, (actualprice.length));
var total = price.match(/[\+\-]*(\d+)\.(\d+)/)[0];
function changePrice(id) {
var select_list_field = document.getElementById(id);
var select_list_selected_index = select_list_field.selectedIndex;
var text = select_list_field.options[select_list_selected_index].text;
var ind = text.indexOf("(") + 1;
var str = text.substring(ind, text.indexOf(")"));
if (str != '') {
str = str.replace(',', '');
str = str.replace('$', '');
str = str.match(/[\+\-]*(\d+)\.(\d+)/)[0];
};
var hFieldId = "hid_" + id;
var hiddenField = document.getElementById(hFieldId).value;
if (str == "choose") {
if (isNaN(hiddenField)) {} else {
total = total - hiddenField;
};
document.getElementById(hFieldId).value = 0;
} else {
document.getElementById(hFieldId).value = str;
if (hiddenField > 0 || hiddenField < 0) {
total = total - hiddenField;
};
if (isNaN(str) || (str.length === 0)) {
total = parseFloat(total);
} else {
total = parseFloat(str) + parseFloat(total);
};
var $total_display = 0;
if (total < 0) {
total_display = 0;
} else {
total_display = total;
}
}
document.getElementById('productPrice').innerHTML = actualprice.replace(/[\+\-]*(\d+)\.(\d+)/, total_display.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,")); //Version 1.2.3
}