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 ?
The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.
How can check CSS property value in jQuery? Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css(“propertyName”);
CSS variables have access to the DOM, which means that you can change them with JavaScript.
Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.
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);
Just assign an object those values and pass it to .css()
var styles;
styles[get_property_name()] = 'red';
$(div).css(styles);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With