Get the node name of the parent element of a <li> element:
var x = document.getElementById("myLI").parentElement.nodeName;
Click the button to get the node name of the parent element of the li element in the list.
<!DOCTYPE html> <html> <body> <p>Example list:</p> <ul> <li id="myLI">Coffee</li> <li>HTML</li> </ul>/*from www.j av a2 s . co m*/ <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>
The parentElement property returns the parent element of the specified element.
The parentElement returns null if the parent node is not an element node:
document.body.parentNode; // Returns the <html> element document.body.parentElement; // Returns the <html> element document.documentElement.parentNode; // Returns the Document node document.documentElement.parentElement; // Returns null (<html> does not have a parent ELEMENT node)
This property is read-only.
The parentElement property returns an Element object representing the parent element node of a node, or null if the node has no parent.