How to create color variables using jQuery?

I am not sure if this is possible. But the concept is similar to SASS variables, but uses jQuery instead. So let's say that I would like to use reusable CSS attributes for jQuery. For instance...

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

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

This obviously does not work, because the syntax is wrong ... but you get the picture, I hope ... So later I could do it ...

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

Is it possible to do something like this?

+3
source share
2 answers

css()can display a map of key-value pairs that represent CSS attributes. This way you can store and use key-value pairs with the required CSS attributes.

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

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

. :

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

:

var mainHeadingStyle = $.extend({'font-size': '50px'}, headingStyle);
$('h1#main').css(mainHeadingStyle);
+8

css(). :

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

All Articles