HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Get a collection of the <body> element's children
The children
property returns a collection of an element's child elements.
The children
property returns an HTMLCollection object.
The firstElementChild Property
The nextElementSibling Property
The previousElementSibling Property
In the HTML DOM (Document Object Model), an HTML document is a collection of nodes with (or without) child nodes.
Whitespace between elements are also text nodes.
Type | Description |
Object | A HTMLCollection object. The collection of element nodes. The elements are sorted as they appear in the document. |
How many children does "myDIV" have:
let count = document.getElementById("myDIV").children.length;
Change the background of the second child element of "myDIV":
const collection = document.getElementById("myDIV").children;
collection[1].style.backgroundColor = "yellow";
Get the text of the third child element (index 2) of a <select> element:
const collection = document.getElementById("mySelect").children[2].text;
Loop all children of <body> and change their background:
const collection = document.body.children;
for (let i = 0; i < collection.length; i++) {
collection[i].style.backgroundColor = "red";
}
element.children
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |