Compares the placement of the specified node with the current node in the DOM hierarchy.
With this method, both element nodes and attribute nodes can be compared and the nodes may belong to different documents.
Another possibility to compare the placement of two element nodes is to use the compareBoundaryPoints method. Create Range objects from the elements first (see the createRange, selectNode and selectNodeContents methods) and use the compareBoundaryPoints method on them.The compareDocumentPosition method is supported in Internet Explorer from version 9. In older versions, you can use the contains method that retrieves whether an element contains another element. If you need a complex comparison method for elements in Internet Explorer, create TextRange objects from the elements first (see the createTextRange and moveToElementText methods) and use the compareEndPoints method on them.
You can find the related objects in the Supported by objects section below.
|Required. Reference to the node to compare.
Integer that retrieves the placement of the specified node relative to the current node in the DOM hierarchy.
Predefined constants are available for the possible returned values, in the scope of the <a href='/lagstsiq.php/#Node'>Node</a> interface. You can use them directly through the Node; interface (Node.DOCUMENT_POSITION_FOLLOWING) or through an instance of the Node object.
The return value can be a combination of the following values (the value of a predefined constant appears in parentheses after the constant in hexadecimal form):
|The specified and the current nodes have no common container node (e.g. they belong to different documents, or one of them is appended to any document, etc.).
|The specified node precedes the current node.
|The specified node follows the current node.
|The specified node contains the current node.
|The specified node is contained by the current node.
|The specified and the current nodes have no common container node or the specified and the current nodes are different attributes of the same node.
It means that when the DOCUMENT_POSITION_DISCONNECTED flag is set then the DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC flag is also set, furthermore the DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC flag is specified for two different attribute nodes of an element.
Example HTML code 1:
This example illustrates the use of the compareDocumentPosition method:
Did you find this example helpful?
User Contributed Comments