Element firstElementChild Property - Javascript DOM

Javascript examples for DOM:Element firstElementChild

Description

The firstElementChild property returns the first child element of an element.

firstChild returns the first child node as an element node, a text node or a comment node depending on which one's first, while firstElementChild returns the first child node as an element node.

This property is read-only.

Return Value

A Node object, representing the first child element of an element, or null if there are no child elements

The following code shows how to get the HTML content of the first child element of an <ul> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<select id="mySelect" size="4">
  <option>Java</option>
  <option>SQL</option>
  <option>HTML</option>
  <option>CSS</option>
  </select>
<br><br>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//www.  java 2s.c  om
    var x = document.getElementById("mySelect").firstElementChild.text;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials