Hi,
i'm trying to add 1 to a value that I fetched from my HTML using jquery.
At the moment it is appending 1, treating the value as a string.
Hi,
i'm trying to add 1 to a value that I fetched from my HTML using jquery.
At the moment it is appending 1, treating the value as a string.
x = parseInt(x)+1 //assuming input in an integer
or
x = parseFloat(x)+1 //assuming input is non-integer
You need to force a type conversion to integer from string.
You want parseInt
. You should provide a base argument, as Paolo mentions, for otherwise it may be interpreted as octal if it has a leading 0
(or hex, if it has a leading 0x
).
js> parseInt("1", 10)+1
2
js> parseInt("010")
8
js> parseInt("0x10")
16
As requested (with jQuery)
$('.vote-count-post').each(function() {
var n = parseInt($(this).text(), 10)+1;
$(this).text(n);
});
This will add one to the element text. In this case, adds one to all votes on this page! (works in firebug - too bad it's only on the page)
Be careful with parseInt
! You should always specify the second argument, which is the base:
$('div.c2 a').text(
parseInt($('div.c2 a').text(),10)+1
);