How to dynamically assigning style properties to element?

Posted by poo on Stack Overflow See other posts from Stack Overflow or by poo
Published on 2010-05-06T09:19:07Z Indexed on 2010/06/06 10:12 UTC
Read the original article Hit count: 328

Filed under:
function styling(elem, props) {
            for (var i in props) {
                if (i == "color") {
                    elem.style.color = props[i].toString();
                }
                if (i == "background") {
                    elem.style.background = props[i].toString();
                }
            }

Using it:

   styling(links, { color: "blue", background: "yellow" });

I'm not really happy with the if-clauses and I want to dynamically add the style properties to the element, but I'm not sure how to do it. Someone out there would know how to do it?

Solved it

  function styling(elem, props) {
        for (var i in props) {
            elem.style[i] = props[i].toString();
        }
    }

That seems to do the trick.

© Stack Overflow or respective owner

Related posts about JavaScript