9

Instead of writing:

$('div').css({'backgroundColor': 'red'});

I want to write something like:

$('div').css({get_property_name(): 'red'});

where get_property_name() will return "backgroundColor", "color", "border-top-color", or any other property.

What options do I have to make it work ?

2 Answers 2

15

The .css() method can also be called as .css(propertyName, value).

$('div').css(get_property_name(), 'red');

If you really need the dictionary representation:

var d = {};
d[get_property_name()] = 'red';
$('div').css(d);
Sign up to request clarification or add additional context in comments.

Comments

1

Just assign an object those values and pass it to .css()

var styles;
styles[get_property_name()] = 'red';
$(div).css(styles);

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.