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.
Example
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>
Syntax
node.compareDocumentPosition(otherNode)
Parameter Values
Value | Type | Explanation |
---|---|---|
otherNode | Required | The other Node with which to compare document positioning. |
Return Values
Value | Explanation |
---|---|
0 | p1 and p2 are same element. |
1 | p1 and p2 do not belong to the same document. |
2 | p1 is positioned after p2. |
4 | p1 is positioned before p2. |
8 | p1 is positioned inside p2. |
16 | p2 is positioned inside p1. |
32 | p1 and p2 has no relationship. |
Reminder
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.