0

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?

2

1 Answer 1

2

Is your toFixed method turning the number into a string? It must be. Try wrapping parseFloat around the entire call.

var newprice = parseFloat(opt1)+parseFloat(opt2)+base;

Also, try testing if opt1 and opt2 are strings

console.log(typeof opt1 === 'string');
Sign up to request clarification or add additional context in comments.

1 Comment

yep, that did it. my javascript is still a bit rough. hadn't fully appreciated how toFixed worked. Thanks muchly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.