
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element children Property
The HTML DOM Element children property is used to retrieve a collection of its child elements (or HTMLCollection), represented as an HTMLCollection object.
The returned collection does not include text nodes and other non-element nodes.
Syntax
Following is the syntax of the HTML DOM Element children property −
element.children;
Parameters
Since this is a property, it will not accept any parameter.
Return Value
The property returns an HTMLCollection object that contains a collection of the child elements of the parent element.
Example 1: Retrieving Children of a Div Element
The following is a basic example of the HTML DOM Element children property. It retrieves the child of a <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element children</title> </head> <body> <h3>HTML DOM Element children Property</h3> <p>It retrieves child elements of the parent ...</p> <div id="parent"> <p>Child 1</p> <p>Child 2</p> <p>Child 3</p> </div> <p id="output"></p> <script> const pE=document.getElementById('parent'); const children = pE.children; // Displaying child elements in the output div let res="<p><strong>Child Elements:</strong></p>"; for (let i = 0; i < children.length; i++) { res += `<p>${children[i].textContent}</p>`; } document.getElementById('output').innerHTML=res; </script> </body> </html>
The above program displays all the child elements of a "div" element.
Example 2: Counting the Number of Children of Div
Here is another example of the HTML DOM Element children property. We use this function to retrieve, the total number of children of a <div> element −
<!DOCTYPE html> <html> <head> <title>HTML DOM Element children</title> </head> <body> <h3>HTML DOM Element children Property</h3> <p>Displays the number of childrens...</p> <div id="myDIV"> <p>Paragraph 1</p> <p>Paragraph 2</p> <p>Paragraph 3</p> <p>Paragraph 4</p> </div> <button onclick="countChildren()">Count Children</button> <div class="output"> <p>Number of children:- <span id="Cc"></span></p> </div> <script> function countChildren() { let myDiv = document.getElementById("myDIV"); let childC = myDiv.children.length; document.getElementById("Cc").textContent = childC; } </script> </body> </html>
When the button clicks, the total number of children of a "div" element will be displayed.
Example 3: Changing Child Elements' Background Color
In the example below, we use the children property to access and change the background of all child elements within a <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element children</title> </head> <body> <h3>HTML DOM Element children Property</h3> <p>It Changes background colour for the Children</p> <div id="pt"> <div>Child 1</div> <div>Child 2</div> <div>Child 3</div> </div> <button onclick="changeBackground()">Change children Background</button> <script> function changeBackground() { const parentDiv=document.getElementById('pt'); const children = parentDiv.children; for (let i = 0; i < children.length; i++) { const child = children[i]; child.style.backgroundColor = '#ff80f0'; } } </script> </body> </html>
When the button is clicked the background color of all child of a "div" element will be changed.
Example 4: Text of Third Child Element in Select Box
This example shows how to interact with dropdowns and retrieve specific elements' content based on their index using children properties. It displays the text of the third child element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element children</title> </head> <body> <h3>HTML DOM Element children Property</h3> <p>Get Text of Third Child Element in Select</p> <select id="mySelect"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select> <button onclick="getThirdChild()">Get Text of Third Child</button> <p id="result"></p> <script> function getThirdChild() { const s = document.getElementById('mySelect'); const thirdText = s.children[2]; document.getElementById('result').textContent = `Text of third child element: ${thirdText.innerHTML}`; } </script> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
children | Yes | Yes | Yes | Yes | Yes |