I have a problem with styles in JavaScript.
I have a object like: styles = {background:'blue',width:'1000px'}
I need write function which takes every attributes from styles object and apply it as a CSS selectors of some HTML element.
something like:
function stylize(def){
for(var key in def){
document.getElementById("test").style.***SELECTOR***=def[key];
}
}
but how get style selector by value of key value?
I need pure JS solution withour jquery etc.
Just use the square bracket notation:
function stylize(def) {
for (var key in def) {
document.getElementById("test").style[key] = def[key];
}
}
Note: this is just an example; not production code.
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