/

HTML / CSS / JavaScript Tutorial

JavaScript DOM HTML method: compareDocumentPosition()

[this page | pdf | back links]

The compareDocumentPosition() method (when applied to HTML elements in the JavaScript DOM) compares the position in the document of two elements. It involves the sum of the following:

 

Contribution

Meaning

1

No relationship: n1 and n2 don’t belong to same document

2

n1 after n2

4

n1 before n2

8

n1 inside n2

16

n2 inside n1

32

No relationship or the two nodes are attribute nodes of the same element

 

where n1 and n2 would be nodes in the form: n1.compareDocumentPosition(n2)

 

It has the following syntax with the following parameters (when applied to elements). It returns a number.

 

element.compareDocumentPosition(node)

 

Parameter

Required / Optional

Description

node

Required

The node object to compare with the current node or element

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile