The previousElementSibling property in HTML DOM is used to return the previous element of the same level of the given element. If no previous element found on the same level then it returns null. It is a read-only property. It is similar to previousSibling property but the difference is previousSibling property returns the previous sibling node as an element node, a text node or a comment node, while previousElementSibling property returns the previous sibling as an element node.
Syntax:
node.previousElementSibling
Return value: This property returns a previous sibling of the specified element or null if the current element has no previous sibling.
Example:
<!DOCTYPE html> <html> <head> <title> HTML DOM previousElementSibling Property </title> </head> <body> <h2> DOM previousElementSibling Property </h2> <h4 id = "h42">Web Languages:</h4> <select size = "4"> <option>HTML</option> <option>CSS</option> <option>JavaScript</option> <option id = "Select">XML</option> </select> <br><br> <button onclick = "Geeks()"> Previous Element Sibling </button> <br><br> <p id = "p"></p> <script> function Geeks() { var a = document.getElementById("Select").previousElementSibling; document.getElementById("p").innerHTML = a.text; } </script> </body> </html> |
Output:
Before click on the button:

After click on the button:

Note: Don’t use white space between two sibling elements, otherwise, the result will be “undefined”.
Supported Browsers: The browser supported by DOM previousElementSibling property are listed below:
- Google Chrome 2.0
- Internet Explorer 9.0
- Firefox 3.5
- Opera 10.0
- Safari 4.0
Recommended Posts:
- 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
- HTML | DOM Base target 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.


