X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

XML DOM compareDocumentPosition() Method


Node Object Reference Node Object

Definition and Usage

The compareDocumentPosition() method compares the placement of two nodes in the DOM hierarchy (document).


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The compareDocumentPosition() method is supported in all major browsers.

Note: Internet Explorer 9 and earlier do not support this method.


Syntax

nodeObject.compareDocumentPosition(node)

Parameters

Parameter Type Description
node Node object Required. Specifies the node to compare with the current node

Return Value

Type Description
Number A Number representing where the two nodes are positioned compared to each other. The possible return values are:

1 - No relationship, the two nodes do not belong to the same document.

2 - The specified node precedes the current node.

4 - The specified node follows the current node.

8 - The specified node contains the current node.

16 - The specified node is contained by the current node.

32 - The specified and the current node have no common container node or the two nodes are different attributes of the same node.

Note: The return value could also be a combination of values. E.g. a return value of 20 means that the specified node is contained by the current node (16) AND the specified node follows the current node (4).

Technical Details

DOM Version Core Level 3 Node Object


Example

The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and compares the placement of two nodes (the first and the third <book> element) in the DOM hierarchy:

Example

xmlDoc = loadXMLDoc("books.xml");

x = xmlDoc.getElementsByTagName('book')[0];
y = xmlDoc.getElementsByTagName('book')[2];

document.write(x.compareDocumentPosition(y));

Output:

4

Try it yourself »


Node Object Reference Node Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook