How to get the child element of a parent using JavaScript ?
Given an HTML document and the task is to select a particular element and get all the child element of the parent element with the help of JavaScript.
Approach 1:
- Select an element whose child element is going to be selected.
- Use .children property to get access of all the children of element.
- Select the particular child based on index.
Example 1: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> Finding child element of parent with pure JavaScript. </title> <style> .parent { background: green; color: white; } .child { background: blue; color: white; margin: 10px; } </style> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <div class="parent" id = "parent"> Parent <div class="child"> Child </div> </div> <br> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "font-size: 24px; font-weight: bold; color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); up.innerHTML = "Click on the button select the child node."; function GFG_Fun() { parent = document.getElementById('parent'); children = parent.children[0]; down.innerHTML = "Text of child node is - '" + children.innerHTML + "'"; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Select the parent element whose child element is going to be selected.
- Use .querySelector() method on parent.
- Use the className of child to select that particular child.
Example 2: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to get the child element of a parent using JavaScript ? </title> <style> .parent { background: green; color: white; } .child1 { background: blue; color: white; margin: 10px; } .child2 { background: red; color: white; margin: 10px; } </style> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <div class="parent" id = "parent"> Parent <div class="child child1"> Child1 </div> <div class="child child2"> Child2 </div> </div> <br> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "font-size: 24px; font-weight: bold; color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); up.innerHTML = "Click on the button select the child node."; function GFG_Fun() { parent = document.getElementById('parent'); children = parent.querySelectorAll('.child'); down.innerHTML = "Text of child node is - '" + children[0].innerHTML + "' and '" + children[1].innerHTML + "'"; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to remove parent element except its child element using jQuery ?
- jQuery | parent > child Selector
- How to select all children of an element except the last child using CSS?
- JQuery | Get the n-th level parent of an element
- How to get the child node index in JavaScript?
- Remove all the child elements of a DOM node in JavaScript
- How to set the value of a select box element using JavaScript?
- How to get the type of DOM element using JavaScript?
- Change an element class JavaScript
- How to get the data attributes of an element using JavaScript ?
- How to check if an element has any children in JavaScript ?
- How to remove an HTML element using JavaScript ?
- How to append data to <div> element using JavaScript ?
- Print the content of a div element using JavaScript
- JavaScript | Get the text of a span element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



