Javascript examples for DOM:Element children
The children property returns a collection of an element's child elements, as an HTMLCollection object.
childNodes contain all nodes, including text nodes and comment nodes, while children only contain element nodes.
A HTMLCollection object, representing a collection of element nodes.
The following code shows how to get a collection of the <body> element's children:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">change the background color of all child elements of body</button> <h2>I am a h2 element</h2> <div>I am a div element</div> <span>I am a span element</span> <script> function myFunction() {//from w w w. ja v a 2 s . c o m var c = document.body.children; var i; for (i = 0; i < c.length; i++) { c[i].style.backgroundColor = "red"; } } </script> </body> </html>