JavaScript node.compareDocumentPosition() Method

You are Here:

JavaScript node.compareDocumentPosition() Method

The node.compareDocumentPosition() method compares two given nodes, and returns an integer describing where they are positioned in the document.


HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point1">I'm paragraph 1</p> <p id="point2">I'm paragraph 2</p> <button onclick="myFunction()">Click Me</button> <script> var p1 = document.getElementById("point1"); var p2 = document.getElementById("point2"); function myFunction(){ alert(p1.compareDocumentPosition(p2)); } </script> </body> </html>



Parameter Values

otherNodeRequiredThe other Node with which to compare document positioning.

Return Values

0p1 and p2 are same element.
1p1 and p2 do not belong to the same document.
2p1 is positioned after p2.
4p1 is positioned before p2.
8p1 is positioned inside p2.
16p2 is positioned inside p1.
32p1 and p2 has no relationship.


Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author