How to select all children of an element with javascript and change CSS property?
Solution 1:
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
Solution 2:
Try to use below codes:
var nodes = document.getElementById('ID_of_parent').getElementsByTagName("div");
for(var i=0; i<nodes.length; i++) {
nodes[i].style.background = color;
}