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.
Syntax:
node.previousSibling
Return value: This property returns a previous sibling of the specified node or null if the current node has no previous sibling.
Example:
<html> <head> <title>DOM previousSibling Property</title> </head> <body style = "text-align: center"> <h1 style = "color:green"> GeeksforGeeks </h1> <h2> DOM previousSibling Property </h2> <div> <span id="p1">GeeksforGeeks! </span><span id="p2"> A computer science portal for geeks. </span> </div> <br> <button onclick="geek()">Click me!</button> <br><br> <p id="p" style = "margin:auto; width: 40%"></p> <!-- Function to return the previous node.--> <script> function geek() { var x = document.getElementById("p2").previousSibling.innerHTML; document.getElementById("p").innerHTML = x; document.getElementById("p").style.color = "white"; document.getElementById("p").style.background = "green"; } </script> </body> </html> |
Output:
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
Recommended Posts:
- HTML DOM TreeWalker previousSibling() Method
- HTML | DOM lang Property
- HTML | DOM Style backgroundClip Property
- HTML | DOM Style borderTop Property
- HTML | DOM Meter max Property
- HTML | DOM Input Hidden value Property
- HTML | DOM Textarea autofocus Property
- HTML | DOM Style columnGap Property
- HTML | DOM Input Time autofocus Property
- HTML | DOM Input Submit formTarget Property
- HTML | DOM Style boxSizing Property
- HTML | DOM Video seekable Property
- HTML | DOM Input Checkbox disabled Property
- HTML | DOM Source src Property
- HTML | DOM Input Range max Property
- HTML | DOM Video loop Property
- HTML | DOM Button disabled Property
- HTML | DOM Input URL size Property
- HTML | DOM Style borderImageSource Property
- HTML | DOM Style wordBreak Property
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.


