whois42
whois42

Reputation: 186

Set style of element from object


I have a problem with setting styles for element from object. In first function I parse string with style code.

parseCheckingStyle: function(properties) {
    var propertiesList = {},
    tempArr=properties.split(/\s*;\s*/, this.length);
    tempArr.forEach(function(element, value, array){
        var splittedTempArr = element.split(/\s*:\s*/);
        propertiesList[splittedTempArr[0]] = splittedTempArr[1];
    });
    return propertiesList;
};

For example

parse('background: #ccc; color: #fff;')

return

{background: "#ccc", color: "#000"}

Then, with this function i try to set this object like style of element:

setCheckingStyle: function(selector, properties) {
    var propertiesList = this.parseCheckingStyle(properties);
    for (var key in propertiesList){
        selector.style.key = propertiesList[key];

    }
};

But style is not setting. How I can solve this problem?

P.S. I need realization without jQuery

Upvotes: 0

Views: 287

Answers (1)

Barmar
Barmar

Reputation: 780994

It should be selector.style[key] instead of selector.style.key. When you use ., the property name is used literally, not as a variable; you have to use [] to evaluate it as a variable.

You obviously know this already, since you wrote propertiesList[key] instead of propertiesList.key.

Upvotes: 2

Related Questions