Michael Klump
Michael Klump

Reputation: 141

Trying to add value of 2 variables with jQuery

I am trying to modify this slider to create a new total. I have already added an additional field called fees that takes the (amount * (duration * 1.15)). Now I am just stuck in trying to combine the amount + fees to generate a new total. When I do it now, it just concatenates the amount and fees.

var $amount = slider == 1?val:$("#amount").val();
var $duration = slider == 2?val:$("#duration").val();

$fees = "$" + ($amount * $duration)*.15;
$total = "$" + ($amount + ($amount * $duration)*.15);

I know it has something to do with converting the string to value because when I multiply $amount * $fees it does work, but I have been reading SO and jQuery API for the last hour without any progress.

Any help is appreciated! Thanks!

Upvotes: 0

Views: 26

Answers (1)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

Use parseInt() as shown :

var $amount = parseInt(slider) == 1?parseInt(val):parseInt($("#amount").val());

or parseFloat() as shown :

var $amount = parseFloat(slider) == 1?parseFloat(val):parseFloat($("#amount").val());

Upvotes: 1

Related Questions