XML DOM previousSiblingProperty
Node object
Definition and Usage
The previousSibling property returns the node immediately preceding an element (at the same tree level).
If no such node exists, this property returns null.
Syntax
Tips and notes
Note:Firefox and most other browsers treat empty spaces or newlines generated between nodes as text nodes, while Internet Explorer ignores blank text nodes generated between nodes. Therefore, in the example below, we will use a function to check the node type of the previous sibling node.
The node type of an element node is 1, so if the previous sibling node is not an element node, it moves to the next node, and then continues to check whether this node is an element node. This entire process continues until the previous sibling element node is found. Through this method, we can get correct results in all browsers.
Tip:To learn more about browser differences, please visit ourDOM browserChapter.
Example
The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, and get the previous sibling node from the first <author> element:
Example
The above code will output:
Previous sibling: title = Everyday Italian
Try it Yourself »
Try it Yourself
nextSibling - Get the next sibling node of an element node
Node objectother extensions