Javascript DOM HTML Node parentNode Property

Introduction

Get the node name of the parent node of a <li> element:

var x = document.getElementById("myLI").parentNode.nodeName;

Click the button to get the node name of the parent node of the li element in the list.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>Example list:</p>

<ul>
  <li id="myLI">Coffee</li>
  <li>HTML</li>
</ul>//  w  w  w. j a va  2  s .  c  o m
<button onclick="myFunction()">Test</button>

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

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

</body>
</html>

The parentNode property returns the parent node of the specified node, as a Node object.

In HTML, the document itself is the parent node of the HTML element.

The <HEAD> and <BODY> are child nodes of the HTML element.

This property is read-only.




PreviousNext

Related