JQuery Simple math?
$('#counter').text(function(i,txt) { return parseInt(txt, 10) + 1; });
Example for adding: http://jsfiddle.net/2uBMy/
Example for subtraction: http://jsfiddle.net/2uBMy/1/
To add a check to a negative, you can do this:
$('#counter').text(function(i,txt) {
var result = parseInt(txt, 10) - 1;
return (result > 0) ? result : 0;
});
$('#counter').text(function(i, txt) {
return +txt + 1;
});
Thus, the contents of the #counterconverted to an integer. This works fine for numbers, but if for some reason something like "foo123" is content, it will become NaN.
, - .parseInt()
$('#counter').text(function(i, txt) {
return parseInt(txt, 10) + 1;
});
parseInt() , ( ). "foo123", "123". , , , .
: http://www.jsfiddle.net/Mtvju/
Ref.: . text()