Reputation: 157
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
Reputation: 34107
parseInt
http://www.minihowtos.net/jquery-parseint
code
alert(" ===> " + (parseInt($("#hid_count").val()) - 1));
Upvotes: 0
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
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