HTML DOM parentNode Property

Last Updated : 22 Aug, 2026

The parentNode property is used to return the parent node of the specified node as a Node object. It is a read-only property. 

Syntax

node.parentNode

Return value: This property returns a parent element of the specified node or null if the current node has no parent element. 

Example: In this example, we will use the parentNode property

HTML
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
    <title>
        DOM parentNode Property
    </title>
</head>
<body onload="start ()" style="text-align: center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>
        DOM parentNode Property
    </h2>
    <button onclick="geek ()">Click me!</button>
    <br>
    <br>
    <div id="container">
    </div>
<!--Driver Code Ends-->

    <script>
        let Text = null;
        function start() {
            Text = document.createElement("span");
            Text.style.color = "green";
            Text.innerHTML = "GeeksforGeeks";
        }
        function geek() {
           let container =
                document.getElementById("container");
            if (Text.parentNode === container) {
                container.removeChild(Text);
            } else {
                container.appendChild(Text);
            }
        }
    </script>

<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
  • It returns the parent Node of any type (Element, Document, or DocumentFragment). This makes it more general than parentElement, which only returns an Element (or null if the parent is not an Element). Document and DocumentFragment nodes themselves always have parentNode === null.
  • It returns null if the node is not attached to a tree (for example, a newly created node that has not yet been inserted, or a node that has been removed).
Comment