HTML | DOM compareDocumentPosition() Method
The DOM compareDocumentPosition() method is used to compare two nodes and it returns an integer describing where they are positioned in the document.
Return Value : This return an integer value and their meaning as follows :
- 1: This means that the two nodes do not belong to the same document.
- 2: This means that the two nodes node1 is positioned after node2.
- 4: This means that the two nodes node1 is positioned before node2.
- 8: This means that the two nodes node1 is positioned inside node2.
- 16: This means that the two nodes node2 is positioned inside node1.
- 32: This means that the two nodes have no relationship or they are two attributes on the same element.
Example-1: This will return only single value.
Before click on the Button:
After click on the button:
Example-2: This will return combination of two values.
Before Click on the Button:
After Click on the Button: Answer will be 20. ‘4’ means that first node is positioned before the second node and ’16’ second node is positioned inside the first node.
Note: The return value can be a combination of values. i.e. if the return value is 20 that means p2 is inside p1, ’16’ and p1 is positioned before p2 ‘4’.
- Google Chrome 1 and above
- Edge 12 and above
- Internet Explorer 9 and above
- Firefox 1 and above
- Opera 12.1 and above
- Safari 4 and above