The firstElementChild property returns the first child element of the specified element.
Get the HTML content of the first child element of an <ul> element:
var x = document.getElementById("myList").firstElementChild.innerHTML;
Click the button to get the HTML content of the list's first child element.
<!DOCTYPE html> <html> <body> <p>Example list:</p> <ul id="myList"> <li>CSS</li> <li>HTML</li> </ul>// w w w .j ava 2s.c om <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var list = document.getElementById("myList").firstElementChild.innerHTML; document.getElementById("demo").innerHTML = list; } </script> </body> </html>
The firstChild returns the first child node as an element node, a text node or a comment node.
The firstElementChild returns the first child node as an element node not including text and comment nodes.
This property is read-only.
Use the children property to return any child element of a specified element.
children[0] will produce the same result as firstElementChild.
To return the last child element of a specified element, use the lastElementChild property.
The firstElementChild property returns a Node object representing the first child element of an element, or null if there are no child elements.