HTML DOM Element children property
- Previous page childNodes
- Next page classList
- Go back to the previous level HTML DOM Elements Object
Definition and usage
children
Property returns a collection of the element's child elements.
children
Property returns an HTMLCollection object.
See also:
HTML nodes and elements
In HTML DOM(Document Object Model), an HTML document is a collection of nodes that have (or do not have) child nodes.
NodeRefers to element nodes, text nodes, and annotation nodes.
ElementThe blank spaces between them are also text nodes.
While elements are just element nodes.
Child nodes and child elements
childNodes ReturnsChild nodes(element nodes, text nodes, and annotation nodes).
children ReturnsChild elements(not text and annotation nodes).
Siblings and element siblings
siblingAre 'brother' and 'sister'.
siblingare nodes that have the same parent node (in the same childNodes list).
Element siblingsare elements that have the same parent element (in the same children list).
Example
Example 1
Get the collection of child elements of the <body> element:
const collection = document.body.children;
Example 2
How many child elements does "myDIV" have:
let count = document.getElementById("myDIV").children.length;
Example 3
Change the background of the second child element of "myDIV":
const collection = document.getElementById("myDIV").children; collection[1].style.backgroundColor = "yellow";
Example 4
Get the text of the third child element (index 2) of the <select> element:
const collection = document.getElementById("mySelect").children[2].text;
Example 5
Traverse all child elements of <body> and change their background:
const collection = document.body.children; for (let i = 0; i < collecton.length; i++) { collection[i].style.backgroundColor = "red"; }
Syntax
element.children
Return value
Type | Description |
---|---|
Object |
HTMLCollection object. A collection of element nodes. Elements are sorted in the order they appear in the document. |
Browser support
element.children
It is a DOM Level 1 (1998) feature.
All browsers fully support it:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
- Previous page childNodes
- Next page classList
- Go back to the previous level HTML DOM Elements Object