Skip to content
Related Articles

Related Articles

Improve Article
HTML | DOM previousSibling Property
  • Last Updated : 26 Jul, 2019

The previousSibling property is used to return the previous node of the specified node as Node object or null if the specified node is the first in the list. It is a read-only property.


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

       <title>DOM previousSibling Property</title>
    <body style = "text-align: center">
        <h1 style = "color:green">
            DOM previousSibling Property
            <span id="p1">GeeksforGeeks! </span><span id="p2">
               A computer science portal for geeks.
        <button onclick="geek()">Click me!</button>
        <p id="p" style = "margin:auto; width: 40%"></p>
        <!-- Function to return the previous node.-->
        function geek() {
          var x = 
          document.getElementById("p").innerHTML = x;
          document.getElementById("p").style.color = "white";
          document.getElementById("p").style.background = "green";

Before clicking the button:
After clicking the button:

Note: Don’t put whitespace between two sibling elements, else the result will be “undefined”.

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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :