imp
imp

Reputation: 470

Calculate with string jQuery

So I've got a span that goes like this:

 <span>1</span>

Now I want to do something like this (plus it)

 $(this).next("span").text($(this).next("span").text + 1);

But it won't work. You're seeing where I'm going though, right? I want to make the number in the string or variable go one higher.

How can I do that?

Upvotes: 1

Views: 2019

Answers (2)

j08691
j08691

Reputation: 207923

$('span').html( parseInt( $('span').html(),10 ) + 1 );

jsFiddle example

Upvotes: 6

Palpatim
Palpatim

Reputation: 9272

Try the JavaScript parseInt() function. Be sure you properly handle error cases, as in someone putting an invalid integer value into the span.

Upvotes: 4

Related Questions