Jan Henning
Jan Henning

Reputation: 82

Subtract 1 from variable jQuery

Having some trouble getting this right. I'm very new to jQuery, so trying to get better and learn.

Currently I am getting 2 different values from a html table using the following code

var sellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var buyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();

These both output a value such as $13,000,000
I am then wanting to subtract 1 from these values (making it $12,999,999) before pasting them to an input as such

$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);

However, I am having some trouble with how to subtract $1 from these. I tried using sellPrice--; but without success. I've also tried adding - 1; at the end of each variable, but did not succeed either.

I tried to test something like this, but did not work either.

var minusOne = -1;
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();

var sellPrice = (getCurrentSellPrice - minusOne);
var buyPrice = (getCurrentBuyPrice - minusOne);

$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);`

Trying my best to familiarize myself with jQuery :)
Any help is much appreciated!

Upvotes: 1

Views: 733

Answers (2)

Jan Henning
Jan Henning

Reputation: 82

Solved using this

var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();

var sellPrice = Number(getCurrentSellPrice.replace(/[^0-9\.]+/g,"")) - 1;
var buyPrice = Number(getCurrentBuyPrice.replace(/[^0-9\.]+/g,"")) + 1;

$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);

Upvotes: 1

Jay Bhatt
Jay Bhatt

Reputation: 5651

Since your numbers contain currency symbol and are strings, you need to convert them to proper numbers before subtracting them. See the answer below.

How to convert a currency string to a double with jQuery or Javascript?

Upvotes: 0

Related Questions