How to get the child node index in JavaScript? Last Updated : 23 Jan, 2023 Comments Improve Suggest changes Like Article Like Report The task is to get the index of child elements among other children. Here are a few techniques discussed. Approach 1: Select the child element of the parent element.Select the parent by .parentNode property.Use Array.prototype.indexOf.call(Children_of_parent, current_child) to get the index. Example 1: This example using the approach discussed above. html <style> .parent { background: green; color: white; } #child1 { background: blue; color: white; margin: 10px; } #child2 { background: red; color: white; margin: 10px; } </style> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <div class="parent" id="parent"> Parent <div id="child1"> Child1 </div> <div id="child2"> Child2 </div> </div> <br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); up.innerHTML = "Click on the button get the index of child element."; function GFG_Fun() { var child = document.getElementById('child2'); var parent = child.parentNode; down.innerHTML = "The index of element with id = 'child2' is = " + Array.prototype.indexOf.call(parent.children, child); } </script> Output: How to get the child node index in JavaScript? Approach 2: Select the child element of the parent element.First, select the parent and then select all children of the parent element.make an array of children and use the indexOf() method to get the index. Example 2: This example uses the approach discussed above. html <style> .parent { background: green; color: white; } #child1 { background: blue; color: white; margin: 10px; } #child2 { background: red; color: white; margin: 10px; } </style> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <div class="parent" id="parent"> Parent <div id="child1"> Child1 </div> <div id="child2"> Child2 </div> </div> <br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); up.innerHTML = "Click on the button get the index of child element."; function GFG_Fun() { var child = document.getElementById('child2'); down.innerHTML = "The index of element with id = 'child2' is = " + Array.from(child.parentNode.children).indexOf(child); } </script> Output: How to get the child node index in JavaScript? Comment More infoAdvertise with us Next Article How to get the child node index in JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Get Selected Index on Mouseover in JavaScript ? To obtain the selected index on mouseover in JavaScript, attach event listeners to the parent container and utilize iteration through child elements to discover the index of the hovered element. By tracking the index of the hovered element during this process, you can determine the selected index ac 2 min read How to get the child element of a parent using JavaScript ? To get a child element of a parent element in JavaScript, you can use several DOM (Document Object Model) methods, such as children, childNodes, querySelector(), or getElementById(). Here are different ways to access child elements based on the situation.1. Using children propertyThe children proper 4 min read How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object 4 min read How to find the position of HTML elements in JavaScript ? In this article, we are given an HTML document and the task is to get the position of any element by using JavaScript. Use the following steps to get the position: Step 1: Selecting an element: Before finding the position, it is necessary to select the required HTML element. Every element in HTML is 3 min read Print the nodes having exactly one child in a Binary tree using JavaScript Given a binary tree, our task is to return the number of nodes in the Binary tree that have at least one child otherwise return â-1â if no such node exists. Examples: Input: 1 / \ 2 3 / \ 4 5 / 6Output: 3Explanation:There are three nodes in the Binary tree that have at least one child that are 1,2,4 4 min read Like