My way of doing this hasn't worked for me yet because every time I try and set the variable to the element, it just makes elem.style.property
equal to value. var elem = document.getElementById('cssTest');
is the element, and elem.style.backgroundColor = value;
is what I am setting equal to. You would think that the value would just apply, even if backgroundColor was the value of another variable, but no, it just makes the entire thing equal value. So if value equals a hexadecimal value, it sets elem.style.property
to that value, literally. The CSS doesn't update to fit the value and then it just fails. That would be my way of telling you to fix it, but I can't get it to work myself. Anyone know how to set it using the below code?
var elem = document.getElementById('cssTest');
var property = 'backgroundColor';
var value = '#ffffff';
elem.style.property = value;
That just doesn't work. It might be because property is a string, but I don't know how to convert it. Do I just get rid of the quotes around the assignment value? I do have a function going through a JSON array to find the property first, but I don't know how to have it come out as an object. I will put a sample below. Please let me know what to fix!
var cssProps = [
{"prop":"background-color","jsprop":"backgroundColor"},
{"prop":"margin-top","jsprop":"marginTop"},
{"prop":"padding-bottom","jsprop":"paddingBottom"}
];
function parseCSS(property,array) {
for (var i = 0;i < array.length;i++) {
if(property == array[i].prop) {
return array[i].jsprop;
};
}
}
var output = parseCSS('background-color',cssProps);
console.log(output);