Reputation: 4514
The code bellow does not work, obviously objects can't be saved to variables in that manner!
function styleElement(aNode){
var cssProperty; var cssValue;
for(var c=0; c<2; c++){
cssProperty = c ? backgroundColor : color;
cssValue = c ? 'blue' : '#fff';
aNode.style.cssProperty = cssValue;
}
Would somebody show me the right way? 10x and BR, Stephan
Upvotes: 1
Views: 335
Reputation: 344537
You need to use bracket notation and strings:
function styleElement(aNode){
var cssProperty; var cssValue;
for(var c=0; c<2; c++){
cssProperty = c ? "backgroundColor" : "color";
cssValue = c ? 'blue' : '#fff';
aNode.style[cssProperty] = cssValue;
}
}
Upvotes: 4