tags:

views:

25

answers:

1
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.

A: 

Use jQuery, it'll solve all of your problems :) http://api.jquery.com/addClass/

Tim
I want to work with native prototypes and javascript is beautiful as it is.
poo
I agree with @poo. As much as I like working with jQuery, questions about native Javascript are allowed here. ;P
deceze