Element parentElement Property - Javascript DOM

Javascript examples for DOM:Element parentElement

Description

The parentElement property returns the parent element.

parentElement returns null if the parent node is not an element node:

This property is read-only.

Return Value

An Element object, representing the parent element node of a node, or null if the node has no parent

The following code shows how to get the node name of the parent element of a <li> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Example list:</p>

<ul>
  <li id="myLI">Coffee</li>
  <li>B</li>
</ul>/* ww  w.  j  a v a2s  .com*/

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

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

<script>
function myFunction() {
    var x = document.getElementById("myLI").parentElement.nodeName;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials