teva
teva

Reputation: 117

jQuery VAT calculation

I'm trying to calculate VAT from value in span, which works, but only if value inside span is used with dot. If i use comma, it shows NaN

JS

$(document).ready(function () {
$("#vatCalculate").ready(function () {
    var vatTextBox = $("#vatPrice").text();
    var op = (vatTextBox * 100) / 122;
    var findVat = vatTextBox - op;
    $("#vatValue").text(findVat.toFixed(2));
});
});

HTML

<span id="vatPrice">30.33</span>
<span id="vatValue"></span>

http://jsfiddle.net/csgz0qmu/

Tnx

Upvotes: 1

Views: 1328

Answers (2)

Daniel B
Daniel B

Reputation: 8879

Thats because vatTextBox is a string, not a number.

Use parseFloat() to parse it, and replace the , characters with a .

Upvotes: 0

Darren Willows
Darren Willows

Reputation: 2131

To remove the commas, you'll need to use replace on the string. To convert to a float so you can do the maths, you'll need parseFloat:

var vatTextBox = parseFloat(vatTextBox.replace(/,/g, ''))

You really should use hidden inputs instead as well and have it stored as a double.

JSFiddle

Upvotes: 3

Related Questions