HTML | DOM parentNode Property

The parentNode property is used to return the parent node of the specified node as 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>
    <script>
        var Text = null;
  
        // function to call on body load
        function start() {
            // creating a span element
            Text = document.createElement("span");
            Text.style.color = "green";
            Text.innerHTML = "GeeksforGeeks";
        }
  
        // check function
        function geek() {
            var container = document.getElementById("container");
            // checking if parent node of Text 
            // matches with that of var container 
            if (Text.parentNode === container) {
                container.removeChild(Text);
            } else {
                container.appendChild(Text);
            }
        }
    </script>
</body>
  
</html>

chevron_right


Output:
Before clicking on the button:
parentnode
After clicking on the button:
parentnode
Click the button again to hide the text.

Supported Browsers: The browser supported by parentNode property are listed below:

  • Apple Safari
  • Google Chrome 1.0
  • Firefox 1.0
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.