How to select all children of an element with javascript and change CSS property?

While this can be done in one line with JQuery, I am assuming you are not using JQuery – in which case, your code will be:

var nodes = document.getElementById('ID_of_parent').childNodes;
for(var i=0; i<nodes.length; i++) {
    if (nodes[i].nodeName.toLowerCase() == 'div') {
         nodes[i].style.background = color;
     }
}

See http://jsfiddle.net/SxPxN/ for a quick example I created – Click on “change ’em” to see it in action

Leave a Comment