iWebaholic
iWebaholic

Reputation: 13

Inserting a variable into jQuery .css

I have a JavaScript Variable that stores the width of an element ,called popupWidth, in px, e.g. '200px'. The '' are part of the variable, because jQuery needs them. How can I insert the variable into jQuerys .css?

$(this).css({
   width: + popupWidth,
});

The code above doesn't work.

Upvotes: 1

Views: 1325

Answers (1)

g19fanatic
g19fanatic

Reputation: 10941

Take a look at the jQuery .css() man page... http://api.jquery.com/css/

It shows that you have to enter the css attributes as essentially a JSON type datastructure... not as just pure text.

$(this).css({ width : popupWidth }); 

the above should work for you as you have 'already' added the px to the popupWidth variable

Upvotes: 2

Related Questions