
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM compareDocumentPosition() Method
The HTML DOM compareDocumentPosition() method is used to compare a given node position to any other node in any document. The return type of this method is of type integer to describe their position in the document. The integer return values are as specified −
1: No relationship, the two nodes do not belong to the same document. 2: The first node (para1) is positioned after the second node (para2). 4: The first node (para1) is positioned before the second node (para2). 8: The first node (para1) is positioned inside the second node (para2). 16: The second node (para2) is positioned inside the first node (para1). 32: No relationship, or the two nodes are two attributes on the same element.
Syntax
Following is the syntax for HTML DOM compareDocumentPosition() method −
node.compareDocumentPosition(node)
Here, the node is of type node object and specifies the node that we want to compare with the current node.
Example
Let us look at an example for the compareDocumentPosition() method −
<!DOCTYPE html> <html> <body> <p id="para1">This is a paragraph</p> <p id="para2">This is another paragraph</p> <p>Click the button to compare the position of the two paragraphs.</p> <button onclick="docPosition()">POSITION</button> <p id="Sample"></p> <script> function docPosition() { var p1 = document.getElementById("para1").lastChild; var p2 = document.getElementById("para2").lastChild; var x = p2.compareDocumentPosition(p1); document.getElementById("Sample").innerHTML = x; } </script> </body> </html>
Output
This will produce the following output −
On clicking the POSITION button −
In the above example −
We have first created two
elements with id “para1” and “para2”.
<p id="para1">This is a paragraph</p> <p id="para2">This is another paragraph</p>
We have then created a button POSTION that will execute the docPosition() method upon being clicked by the user −
<button onclick="docPosition()">POSITION</button>
The docPosition() method gets both the <p> element using the getElementById() method on document object. It then assigns the lastchild property value of both the paragraphs to variable p1 and p2 respectively.
We then call the compareDocumentPosition() method on p2 with p1 as the parameter. This means that we want to compare the position of p2 with respect to p1. Since here the p2 is positioned after p1, so the return value is 2 −
function docPosition() { var p1 = document.getElementById("para1").lastChild; var p2 = document.getElementById("para2").lastChild; var x = p2.compareDocumentPosition(p1); document.getElementById("Sample").innerHTML = x; }
- Related Articles
- HTML DOM addEventListener() Method
- HTML DOM normalize( ) Method
- HTML DOM write() Method
- HTML DOM writeln() Method
- HTML DOM insertAdjacentElement( ) Method
- HTML DOM insertAdjacentHTML( ) Method
- HTML DOM insertAdjacentText( ) Method
- HTML DOM insertBefore( ) Method
- HTML DOM isDefaultNamespace( ) Method
- HTML DOM isEqualNode( ) Method
- HTML DOM isSameNode( ) Method
- HTML DOM item( ) Method
- HTML DOM appendChild() Method
- HTML DOM blur() Method
- HTML DOM click() method
