-2

I am wanting to subtract some values of inputs with the total price.

The code:

$('.calculate-resterend').click(function(e) {
    e.preventDefault();
    var contant = $('.checkout-contant').val();
    var pin = $('.checkout-pin').val();
    var creditcard = $('.checkout-creditcard').val();
    var waardebon = $('.checkout-waardebon').val();

    var totalprice = $('.total.final-price.price').text();
    alert(contant - totalprice);
});

But this returns NaN. I figure it's because total price is .text();, but what is the correct way to substract between these things.

Lets say var contant has a value of 2000,98
and the total price has a value of 2400,99

I want it to return 400,01.

vinS
  • 1,417
  • 5
  • 24
  • 37
Rubberduck1337106092
  • 1,294
  • 5
  • 21
  • 38

3 Answers3

6

Use Number()

alert(Number(contant) - Number(totalprice));

If you also want to remove comman(,)

alert(Number(contant.replace(/\,/g,'')) - Number(totalprice.replace(/\,/g,'')));
squiroid
  • 13,809
  • 6
  • 47
  • 67
1

You may have to convert one of your values using parseInt. You can try the following:

totalprice = parseInt(totalprice)

And then proceed to do your simple subtraction like before. You could also try to make your own price attribute on the HTML element itself and the fetch the attribute value instead like .attr("price"). Not sure if that would return a string as well.

sahil-sethi
  • 122
  • 2
  • 8
0

try to convert your value by using parseint()

eg:- parseint(your_value)

Hashan
  • 164
  • 8