HTML DOM compareDocumentPosition()Method
Examples
Compare the document position of the current node with the specified node:
Output result:
Try it Yourself »
Definition and Usage
compareDocumentPosition()The method compares the document position of the current node with the specified node in document order.
Looking at the above example, the return value could be:
1: No relationship, the two nodes do not belong to the same document.
2: The first node (P1) is located after the second node (P2).
4: The first node (P1) is positioned before the second node (P2).
8: The first node (P1) is located inside the second node (P2).
16: The second node (P2) is located inside the first node (P1).
32: No relationship, or the two nodes are two attributes of the same element.
Note:The return value can be a combination of values. For example, a return of 20 means that p2 is inside p1 (16) and p1 is before p2 (4).
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the compareDocumentPosition() method
Note:Internet Explorer 8 and earlier IE versions do not support this method.
Syntax
Parameters
| Parameter | Type | Description |
|---|---|---|
| node | Node object | Required. The specified node you want to compare. |
Return Value
| Type | Description |
|---|---|
| Number | The document position of the current node compared with the specified node Possible return values: Looking at the above example, the return value could be: 1: No relationship, the two nodes do not belong to the same document. 2: The first node (P1) is located after the second node (P2). 4: The first node (P1) is positioned before the second node (P2). 8: The first node (P1) is located inside the second node (P2). 16: The second node (P2) is located inside the first node (P1). 32: No relationship, or the two nodes are two attributes of the same element. |
Technical Details
| DOM Version | Core Level 1 Node Object |
|---|
Other Extensions
Element Object