Getting Child Elements of a Node in JavaScript17 Mar 2025 | 4 min read A parent node in JavaScript may have one or more child elements in JavaScript. Now how to fetch those child elements of a node in JavaScript is the question. In this section, we will discuss and look at how we can get child elements of a node in JavaScript. For accessing the child elements of a parent node in JavaScript, there are the following approaches we can use:
Let's discuss each approach one by one. Example It is an example code that will help us to understand how to fetch the child elements of a particular node: Now, using this example code, we will understand each approach that we have described above. Getting First Child Element of a NodeIn order to get the first child element of a node, it is required to use the firstChild property of the element. Syntax: In the described syntax, the variable firstchild will hold the value of the first child element, and the parentElement is the parent node of which we are trying to get the first child element value. Points to be noted:
Example Let's see the below code example to understand how to use the firstChild property: In the above code, we have stored the <ul> element id to the frstchild variable, and then we have fetched the first child element value and stored it frstchild variable. Finally, we have the output that child element value. We can see in the below output: ![]() Getting the last child element of a nodeIn order to get the last child element of a node, we need to use the lastChild property. Syntax: The lastChild variable will hold the value of the last child element present in the parent node. Examine the below part of the example code: So, in the above code, we can see that similar to the retrieval of the first child element of a node, we have used the lastElementChild method to fetch the last child. However, if there is no child element present in the parent node, the property returns null. Otherwise, it will return the last child element value if present. We have the following output: ![]() Point to be noted:
Getting all the child elements of a nodeIn order to get all the child elements of a node, use childNodes or children properties. Both these properties can be used to get all the specified children of a parent node, but the difference between both is that the childNodes property will return all the child elements with any node type, i.e., the live NodeList of all child nodes of any node type whereas the children property will return the child element nodes with only the node type. Let's see the syntax for both properties: The childNodes Property The children Property Look at the below example code section to see how to get all the child elements: In the section of code, we are getting all the child elements of the parent node using the children property, as we can see in the below snapshot: ![]() Next TopicJavaScript scrollIntoView |
In this article, we are going to discuss JavaScript animation with its functions. What is ? JavaScript animations are done by incremental programming changes in the style of an element. JavaScript animations can perform tasks that CSS cannot. JavaScript could be used to transfer several DOM elements...
7 min read
The ECMAScript6 (ES6) provides the string repeat() feature to repeat the given string. The string can provide data to display the required time with function and event. The repeat method does not affect the original data, but displays require time the similar data. In other words,...
4 min read
JavaScript code can be run independently in a browser using the open-source, cross-platform Node.js runtime environment. The "npm" represents "Node Package Manager". The Node.js package manager is used to install various internet packages as well as dependencies into a local environment using a command-line tool. It is...
3 min read
The javascript removes the single and multiple classes using a simple method. We can use the query selector with the remove() method. The remove method requires the classList keyword to get the multiple classes. The multiple classes use the "for" loop in the javascript. Syntaxes The syntax...
5 min read
The unique feature of async generator functions in javascript is their support to both await and yield. It returns an async iterator rather than a promise or iterator. The async generator functions are distinct from async functions and the generator functions in javascript. The () function...
3 min read
JavaScript offers in-built methods to open and close the browser window to perform additional operations like robot window etc. These methods help to open or close the browser window pop-ups. Following are the window methods: open() close() The window.open method is used to open a new web page...
6 min read
JavaScript Date.now() Method JavaScript Date.now() function is used to display the time elapsed since January 1, 1970, 00:00:00 UTC. It will return the value in milliseconds. Since now() is a static method of the Date object, it will always be used as Date.now(). We cannot use...
2 min read
Concept Design patterns can be termed as well-documented solutions to the most commonly occurring problems in software engineering. It turns quite hectic for developers to bang out their heads on the problem that someone else has already solved. Every developer aspires to write industrial-level code that...
9 min read
The Element interface's insertAdjacentHTML() method converts the given text to HTML or XML and puts the generated nodes into the DOM tree at a predetermined point. To insert text as an HTML file at a certain point in the DOM, use the insertAdjacentHTML() method. The text...
4 min read
What is "Vanilla JavaScript"? The term vanilla script is used to refer to the pure JavaScript (or we can say plain JavaScript) without any type of additional library. Sometimes people often used it as a joke. Nowadays several things can also be done without using any...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India