jcrowley
jcrowley

Reputation: 71

Increasing css values with jquery

How can I increase css values with jquery?

I want to increase values such as top and left, this is what i tried but does not work:

var left = 5;
$(.object).css("left" + 5);

the thing is that i need the value to be an integer rather than a string like "5px". I need 5 so that i can change it with numerical expressions.

Upvotes: 2

Views: 5819

Answers (3)

Miro
Miro

Reputation: 8650

Yankee is right:

var left = parseInt($(object).css("left")); 

$(object).css('left', left+5);

Upvotes: 2

freejosh
freejosh

Reputation: 11383

From .css() documentation:

As of jQuery 1.6, .css() accepts relative values similar to .animate(). Relative values are a string starting with += or -= to increment or decrement the current value. For example, if an element's padding-left was 10px, .css( "padding-left", "+=15" ) would result in a total padding-left of 25px.

So, you can increment left with your js variable with:

var left = 5;
$(object).css('left', '+=' + left);

Upvotes: 14

Linmic
Linmic

Reputation: 791

var left = 5;

$(object).css('left', left+=5);

Upvotes: -1

Related Questions