3

I am not sure if this is possible. But the concept is similar to SASS variables but using jQuery instead. So lets say I wanted to have reusable CSS attributes for jQuery. For example...

I want to make a variable that I can reuse for the color black

var black = .css('color','#000');
var white = .css('color','#FFF');

This obviously doesn't work because the syntax is wrong... but you get the picture I hope.. So then later I could do this...

$('#myelement').css(black);

Is it possible to do something like this?

1
  • Pass an object. var white = {color: "#000"} Commented May 17, 2012 at 21:34

2 Answers 2

8

css() can take a map of key-value pairs that represent the CSS attributes. Therefore, you could store and use key-value pairs with the desired CSS attributes.

var black = {'color': '#000'};
var white = {'color': '#FFF'};

$('#myelement').css(black);

These can contain any number of attributes. For example:

var headingStyle = {'background-color': '#C00', 'color': '#FFF'};
$('h1#heading').css(headingStyle);

You could even create new styles based on a certain style and add attributes to them:

var mainHeadingStyle = $.extend({'font-size': '50px'}, headingStyle);
$('h1#main').css(mainHeadingStyle);
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass an object to you css() call. Do this:

var white = {'color' : '#fff'};
var black = {'color' : '#000'};
$('#myElement').css(white); 
$('#otherElement').css(black);

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.