X10nD
X10nD

Reputation: 22030

add using jquery

I want to do this

var x=$(this).attr('id');
var y = x+1;

where x is an integer

but the value I get is x1

How do I do get the 16, if x=15?

Thanks Jean

Upvotes: 0

Views: 110

Answers (4)

Amarghosh
Amarghosh

Reputation: 59451

console.log(Number("23") + 1);   //24

I think you should be using Number() instead of parseInt because:

console.log(Number("23#") + 1);   //NaN
console.log(parseInt("23#") + 1); //24 (I would expect a NaN)

Upvotes: 1

Dan F
Dan F

Reputation: 12052

All the answers so far are missing the radix parameter

var x=parseInt($(this).attr('id'), 10);
var y = x+1;

Upvotes: 8

Mark Elliot
Mark Elliot

Reputation: 77044

You need to tell JavaScript it's an integer using parseInt

var y = parseInt(x) + 1;

Upvotes: 1

Andrew
Andrew

Reputation: 837

var y = parseInt(x) + 1;

should do the trick.

Upvotes: 2

Related Questions