HTML DOM compareDocumentPosition Method

## JavaScript Reference

Overview

## JavaScript Objects

JS Array JS Boolean JS Date JS Math JS Number JS String JS RegExp JS Global

## Browser Objects

Window Navigator Screen History Location

## HTML DOM Objects

DOM Document DOM Elements DOM Attributes DOM Events

## HTML Objects

DOM Anchor DOM Area DOM Base DOM Body DOM Button DOM Form DOM Frame/IFrame DOM Frameset DOM Image DOM Input Button DOM Input Checkbox DOM Input File DOM Input Hidden DOM Input Password DOM Input Radio DOM Input Reset DOM Input Submit DOM Input Text DOM Link DOM Meta DOM Object DOM Option DOM Select DOM Style DOM Table DOM td / th DOM tr DOM Textarea

# HTML DOM compareDocumentPosition() Method

Element Object

## Example

Find out where one paragraph is postiotioned compared to another paragraph:

var p1=document.getElementById("p1");
var p2=document.getElementById("p2");
p1.compareDocumentPosition(p2);

The result will be:

4
Try it yourself »

## Definition and Usage

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

Look at the example above. The possible return values would specify:

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

2: The first node (p1) is positioned after the second node (p2).

4: The first node (p1) is positioned before the second node (p2).

8: The first node (p1) is positioned inside the second node (p2).

16: The second node (p2) is positioned inside the first node (p1).

32: No relationship, or the two nodes are two attributes on the same element.

Note: The return value could also be a combination of values. I.e. the returnvalue 20 means that p2 is inside p1 (16) AND p1 is positioned before p2 (4).

## Browser Support

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

Note: Internet Explorer 8 and earlier does not support this method.

## Syntax

node.compareDocumentPosition(node)

## Parameters

Parameter | Type | Description
--- | --- | ---
node | Node object | Required. The node you want to compare the specified node with

## Return Value

Type | Description
--- | ---

Number

A Number representing where two nodes are positioned compared to each other

Possible return values:

1 (No relationship, the two nodes do not belong to the same document)

2 (The first node (p1) is positioned after the second node (p2))

4 (The first node (p1) is positioned before the second node (p2))

8 (The first node (p1) is positioned inside the second node (p2))

16 (The second node (p2) is positioned inside the first node (p1))

32 (No relationship, or the two nodes are two attributes on the same element)

## Technical Details

DOM Version | Core Level 1 Node Object

Element Object

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE