Tyler Willingham
Tyler Willingham

Reputation: 559

Can someone REMIND me how to ADD to a .css value using jQuery

I feel silly asking this because I should know it but I'm in a hurry and I also can't for the life of my think of the right term to put into Google to get the answer I'm looking for.

Basically using javascript/jquery I need to say "add varName's value to the existing css property of left:200px;" using the the .css in jQuery.

Upvotes: 0

Views: 823

Answers (4)

ShankarSangoli
ShankarSangoli

Reputation: 69915

Go through this

http://api.jquery.com/css/

//This will set or overwrite the left style property of element 
//selected by elementSelector
$("elementSelector").css("left", varName);

Alternatively css method takes a map if you want to set multiple properties at once.

$("elementSelector").css( { left: varName });

Upvotes: 2

Don Zacharias
Don Zacharias

Reputation: 1564

var extra;
//fiddle with the value of extra
$("#myElement").css("left", 100 + extra +"px");

Upvotes: 0

Joseph Silber
Joseph Silber

Reputation: 219936

Are you trying to increment the current CSS value with the one in your variable?

If so, use this:

var varName = 20;

$('#element').css('left', '+=' + varName);

Upvotes: 2

kiddailey
kiddailey

Reputation: 3664

$('.selector').css('opacity', '0.5');

or, for multiples:

$('.selector').css({'opacity' : '0.5', 'width' : '100px'});

or, for a specific class:

$('.selector').addClass('className');

http://api.jquery.com/css/

http://api.jquery.com/addClass/

Upvotes: 0

Related Questions