Rate this script:  I Love it  /   I Hate it

Style a collection of elements quickly


Code


function setStyles(el, css) {
        var l = el.length;
        if (typeof l == 'undefined') {el = [el]; l = 1;}
        for (var i=0; i<l; i++) {
                for (var s in css) el[i].style[s] = css[s];
        }
}

window.onload = function() {
        // single element
        setStyles(document.body, {background:'yellow'});
        // nodeList
        setStyles(document.getElementsByTagName('div'), {
                background: '#def',
                color: 'red'
        });
        // array
        var a = [document.body, document.getElementsByTagName('div')[0]];
        setStyles(a, {border: '1px black dotted'});
};
 

 

 
Style a collection of elements quickly scripts | Style a collection of elements quickly snippet | Style a collection of elements quickly example | Style a collection of elements quickly tutorial | Style a collection of elements quickly code