0

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.

4
  • read: stackoverflow.com/questions/1306523/… Commented Aug 31, 2011 at 2:56
  • What about .css() function? ;-) Commented Aug 31, 2011 at 2:57
  • No I'm using the .css function. That's not the issue :p It's simpler than that. Commented Aug 31, 2011 at 2:58
  • No I'm using the .css function. That's not the issue :p It's simpler than that. I actually just need to know how actually ADD the value of varName to the EXISTING CSS value Commented Aug 31, 2011 at 2:58

4 Answers 4

2

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 });
Sign up to request clarification or add additional context in comments.

Comments

2

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);

2 Comments

@Joseph Silber I never realized you could do that!
@Don Zacharias: It's a relatively new feature. It was introduced in jQuery 1.6, I think. It's in the docs: api.jquery.com/css (search for +=).
0
$('.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/

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.