HTML DOM Element previousSibling
Example
Return the HTML content of the previous sibling of a list item:
Description
ThepreviousSibling property returns the previous node on the same tree level.
ThepreviousSibling property returns a node object.
ThepreviousSibling property is read-only.
Important!
previousSibling returns the previous siblingnode:An element node, a text node, or a comment node.
Whitespace between elements are also text nodes.
Alternative:
The previousElementSibling Property
previousElementSibling returns the previous siblingelement (ignores text and comments).
See Also:
Nodes vs Elements
In the HTML DOM terminology:
Nodes are all nodes (element nodes, text nodes, and comment nodes).
Whitespace between elements are also text nodes.
Elements are only element nodes.
Siblings vs Element Siblings
Siblings are "brothers" and "sisters".
Siblings are nodes with the same parent (in the samechildNodes list).
Element Siblings are elements with the same parent (in the samechildren list).
childNodes vs children
childNodes returns childnodes (element nodes, text nodes, and comment nodes).
children returns childelements (not text and comment nodes).
nextSibling vs nextElementSibling
nextSibling returns the nextnode (an element node, a text node or a comment node).Whitespace between elements are also text nodes.
nextElementSibling returns the nextelement (not text and comment nodes).
previousSibling vs previousElementSibling
previousSibling returns the previousnode (an element node, a text node or a comment node).Whitespace between elements are also text nodes.
previousElementSibling returns the previouselement (not text and comment nodes).
Syntax
Return Value
| Type | Description |
| Node | The previous sibling of the node.null if no previous sibling exists. |
Browser Support
element.previousSibling is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | 9-11 |

