I’ve got three variables I’m trying to sum the values. For two of these items, I need the form value to read differently, so i’ve created an attribute cost to pull the cost through with. My code is:
var opt1 = parseFloat($('#ac1 option:selected').attr('cost')).toFixed(2);
var opt2 = parseFloat($('#ac2 option:selected').attr('cost')).toFixed(2);
var base = parseFloat($('#original_price').val());
var newprice = opt1+opt2+base;
if opt1 should be 4.00, opt2 6.50 and base 10.00, it’s giving me an output of 4.006.5010.00 instead of 20.50
any ideas on where i’m going wrong?
Is your
toFixedmethod turning the number into a string? It must be. Try wrapping parseFloat around the entire call.Also, try testing if opt1 and opt2 are strings