newprogress
newprogress

Reputation: 157

Subtracting an integer from a value which is fetched using $(#id).val()

In order to subtract an integer from a value which is fetched using $(#id).val() I have tried:

$("#hid_count").val() = $("#hid_count").val() - 1;

and:

count = $("#hid_count").val();

Here hid_count is a hidden field on the page. However, both of these are not working. An error is coming on the page. Can anybody explain why this is?

Upvotes: 0

Views: 1432

Answers (4)

Tats_innit
Tats_innit

Reputation: 34107

working demo click here

parseInt

http://www.minihowtos.net/jquery-parseint

code

alert(" ===> " + (parseInt($("#hid_count").val()) - 1));​

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337590

You will need to use parseInt() to convert the val() from a string to an int, so that the arithmetic operation works correctly.

$("#hid_count").val(parseInt($("#hid_count").val(),10) - 1);

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

$("#hid_count").val(parseInt($("#hid_count").val(), 10) - 1);

Since you are reading a string, you have to make a parseInt before arithmetic operation

Upvotes: 3

OptimusCrime
OptimusCrime

Reputation: 14863

$("#hid_count").val($("#hid_count").val() - 1);

Upvotes: 1

Related Questions