HTML DOM previousSibling Property
Example
Get the HTML content of the previous sibling of a list item:
var x = document.getElementById("item2").previousSibling.innerHTML;
The result of x will be:
Coffee (first li)
Try it Yourself »
Definition and Usage
The previousSibling property returns the previous node of the specified node, in the same tree level.
The returned node is returned as a Node object.
The difference between this property and previousElementSibling, is that previousSibling returns the previous sibling node as an element node, a text node or a comment node, while previousElementSibling returns the previous sibling node as an element node (ignores text and comment nodes).
This property is read-only.
Tip: Use the nextSibling property to return the next node of the specified node, in the same tree level.
Tip: Use the childNodes property to return any child node of a specified node.
Browser Support
Property | |||||
---|---|---|---|---|---|
previousSibling | Yes | Yes | Yes | Yes | Yes |
Syntax
node.previousSibling
Technical Details
Return Value: | A Node object, representing the previous sibling of the node, or null if there is no previous sibling |
---|---|
DOM Version | Core Level 1 Node Object |
Related Pages
HTML DOM reference: node.childNodes Property
HTML DOM reference: node.firstChild Property
HTML DOM reference: node.lastChild Property
HTML DOM reference: node.parentNode Property
HTML DOM reference: node.nextSibling Property
HTML DOM reference: node.nodeName Property