How do I iterate the price in parentheses and add it to the total price, My code doesn't seem to work
I've found this on google and gave it a go. But it seems as if its not doing anything. it isn't updating the price total area at all, nor is it adding or subtracting.
<script type="text/javascript">
function doMath() {
// Capture the entered values of two input boxes
var my_input1 = document.getElementsByName("case").value;
var my_input2 = document.getElementsByName("cpu").value;
var my_input3 = document.getElementsByName("cooling").value;
var my_input4 = document.getElementsByName("ram").value;
var my_input5 = document.getElementsByName("gpuchoice").value;
var my_input6 = document.getElementsByName("storage").value;
var my_input7 = document.getElementsByName("storage1").value;
var my_input8 = document.getElementsByName("storage2").value;
var my_input9 = document.getElementsByName("powersupchoice").value;
var my_input10 = document.getElementsByName("fans").value;
var my_input11 = document.getElementsByName("lighting").value;
var my_input12 = document.getElementsByName("sleeving").value;
var my_input13 = document.getElementsByName("peripherals").value;
var my_input14 = document.getElementsByName("headsets").value;
var my_input15 = document.getElementsByName("controllers").value;
var my_input16 = document.getElementsByName("wirelessadapter").value;
// Add them together and display
var sum = parseInt(my_input1) + parseInt(my_input2) + parseInt(my_input3) + parseInt(my_input4) + parseInt(my_input5) + parseInt(my_input6) + parseInt(my_input7) + parseInt(my_input8) + parseInt(my_input9) + parseInt(my_input10) + parseInt(my_input11) + parseInt(my_input12) + parseInt(my_input13) + parseInt(my_input14) + parseInt(my_input15) + parseInt(my_input16);
document.getElementsByName("text2").value = sum;
document.getElementsByName("text2").innerHTML = sum;
}
</script>
the specific page im trying to get it to work on is: voidtechpcs.com/testbuild.php It's not pretty so be warned, I'm fairly new to this.
Any advice, or a better way of handling this perhaps?
The code works, but it wont get the data price of storage 1 or storage 2, with those two removed from the sum function it adds the totals of everything else perfect. just not these two inputs.
<select name="storage1" id="storage1">
<option data-productprice="45.00" value="None" selected>None</option>
<option data-productprice="45.00" value="Crucial MX500 SSD 500 GB (+$0.00)">Crucial MX500 500 GB (+$0.00)</option>
<option data-productprice="45.00" value="Crucial MX500 SSD 1 TB (+$0.00)">Crucial MX500 1 TB (+$0.00)</option>
<option data-productprice="45.00" value="Crucial MX500 SSD 2 TB (+$0.00)">Crucial MX500 2 TB (+$0.00)</option>
</select>
here is the JS that is broken with the storage options in sum function, remove the parseFloat(my_input7) everything works great. put it back in and NAN haha
function doMath() {
//var regExp = /\(([^)]+)\)/;
//var matches = regExp.exec("I expect five hundred dollars ($500).");
// Capture the entered values of two input boxes
var my_input1 = document.querySelector('[name=case]:checked').dataset.productprice;
var my_input2 = document.querySelector('[name=cpu]:checked').dataset.productprice;
var my_input3 = document.querySelector('[name=cooling]:checked').dataset.productprice;
var my_input4 = document.querySelector('[name=ram]:checked').dataset.productprice;
var my_input5 = document.querySelector('[name=gpuchoice]:checked').dataset.productprice;
var my_input6 = document.querySelector('[name=storage]:checked').dataset.productprice;
var my_input7 = document.querySelector('[name=storage1]').dataset.productprice;
var my_input8 = document.querySelector('[name=storage2]').dataset.productprice;
var my_input9 = document.querySelector('[name=powersupchoice]:checked').dataset.productprice;
var my_input10 = document.querySelector('[name=fans]:checked').dataset.productprice;
var my_input11 = document.querySelector('[name=lighting]:checked').dataset.productprice;
var my_input12 = document.querySelector('[name=sleeving]').dataset.productprice;
var my_input13 = document.querySelector('[name=peripherals]').dataset.productprice;
var my_input14 = document.querySelector('[name=headsets]').dataset.productprice;
var my_input15 = document.querySelector('[name=controllers]').dataset.productprice;
var my_input16 = document.querySelector('[name=wirelessadapter]').dataset.productprice;
// Add them together and display
var sum = parseFloat(55.00) + parseFloat(50.25) + parseFloat(20.55) + parseFloat(my_input1) + parseFloat(my_input2) + parseFloat(my_input3) + parseFloat(my_input4) + parseFloat(my_input4) + parseFloat(my_input6) + parseFloat(my_input7);
document.getElementById('pricetotal').value = sum;
document.getElementById('text2').innerHTML = sum;
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/8q9zHVK
Comments
Post a Comment