Range
The Range interface represents a fragment of a document that can contain nodes and parts of text nodes.
A range can be created by using the Document.createRange() method. Range objects can also be retrieved by using the getRangeAt() method of the Selection object or the caretRangeFromPoint() method of the Document object.
There also is the Range() constructor available.
Properties
There are no inherited properties.
-
Range.collapsedRead only -
Returns a boolean value indicating whether the range's start and end points are at the same position.
-
Range.commonAncestorContainerRead only -
Returns the deepest
Nodethat contains thestartContainerandendContainernodes. -
Range.endContainerRead only -
Returns the
Nodewithin which theRangeends. -
Range.endOffsetRead only -
Returns a number representing where in the
endContainertheRangeends. -
Range.startContainerRead only -
Returns the
Nodewithin which theRangestarts. -
Range.startOffsetRead only -
Returns a number representing where in the
startContainertheRangestarts.
Constructor
Methods
There are no inherited methods.
Range.setStart()-
Sets the start position of a
Range. Range.setEnd()-
Sets the end position of a
Range. Range.setStartBefore()-
Sets the start position of a
Rangerelative to anotherNode. Range.setStartAfter()-
Sets the start position of a
Rangerelative to anotherNode. Range.setEndBefore()-
Sets the end position of a
Rangerelative to anotherNode. Range.setEndAfter()-
Sets the end position of a
Rangerelative to anotherNode. Range.selectNode()-
Sets the
Rangeto contain theNodeand its contents. Range.selectNodeContents()-
Sets the
Rangeto contain the contents of aNode. Range.collapse()-
Collapses the
Rangeto one of its boundary points. Range.cloneContents()-
Returns a
DocumentFragmentcopying the nodes of aRange. Range.deleteContents()-
Removes the contents of a
Rangefrom theDocument. Range.extractContents()-
Moves contents of a
Rangefrom the document tree into aDocumentFragment. Range.insertNode()-
Insert a
Nodeat the start of aRange. Range.surroundContents()-
Moves content of a
Rangeinto a newNode. Range.compareBoundaryPoints()-
Compares the boundary points of the
Rangewith anotherRange. Range.cloneRange()-
Returns a
Rangeobject with boundary points identical to the clonedRange. Range.detach()-
Releases the
Rangefrom use to improve performance. Range.toString()-
Returns the text of the
Range. -
Range.compareNode() -
Returns a constant representing whether the
Nodeis before, after, inside, or surrounding the range. -
Range.comparePoint() -
Returns -1, 0, or 1 indicating whether the point occurs before, inside, or after the
Range. -
Range.createContextualFragment() -
Returns a
DocumentFragmentcreated from a given string of code. -
Range.getBoundingClientRect() -
Returns a
DOMRectobject which bounds the entire contents of theRange; this would be the union of all the rectangles returned byrange.getClientRects(). -
Range.getClientRects() -
Returns a list of
DOMRectobjects that aggregates the results ofElement.getClientRects()for all the elements in theRange. -
Range.intersectsNode() -
Returns a
booleanindicating whether the given node intersects theRange. -
Range.isPointInRange() -
Returns a
booleanindicating whether the given point is in theRange.
Specifications
| Specification |
|---|
| DOM Standard (DOM) # interface-range |
| DOM Parsing and Serialization # extensions-to-the-range-interface |
| CSSOM View Module (CSSOM View) # extensions-to-the-range-interface |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Range |
1 |
12 |
1
Starting with Firefox 13, the
Range object throws a DOMException as defined in DOM 4, instead of a RangeException defined in prior specifications. |
9 |
9 |
1 |
1 |
18 |
4
Starting with Firefox 13, the
Range object throws a DOMException as defined in DOM 4, instead of a RangeException defined in prior specifications. |
10.1 |
1 |
1.0 |
Range |
29 |
15 |
24 |
No |
16 |
8 |
≤37 |
29 |
24 |
16 |
8 |
2.0 |
cloneContents |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
cloneRange |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
collapse |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
collapsed |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
commonAncestorContainer |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
compareBoundaryPoints |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
compareNode |
1-45 |
No |
1-3 |
No |
15-32 |
3 |
1-45 |
18-45 |
No |
14-32 |
1 |
1.0-5.0 |
comparePoint |
1 |
17 |
1 |
No |
≤12.1 |
3 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
createContextualFragment |
1 |
12 |
1 |
11 |
≤12.1 |
≤4 |
1 |
18 |
4 |
≤12.1 |
Yes |
1.0 |
deleteContents |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
detach |
1
Starting in Chrome 37, this method is a no-op and has no effect.
|
12 |
1-15
Starting in Firefox 15.0, this method is a no-op and has no effect.
|
9 |
9
Starting in Opera 24, this method is a no-op and has no effect.
|
1
Since August 2015 this method is a no-op in WebKit-based browsers.
|
1
Starting in WebView 37, this method is a no-op and has no effect.
|
18
Starting in Chrome 37, this method is a no-op and has no effect.
|
4-15
Starting in Firefox 15.0, this method is a no-op and has no effect.
|
10.1
Starting in Opera 24, this method is a no-op and has no effect.
|
1
Since August 2015 this method is a no-op in WebKit-based browsers.
|
1.0
Starting in Samsung Internet 3.0, this method is a no-op and has no effect.
|
endContainer |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
endOffset |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
extractContents |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
getBoundingClientRect |
4 |
12 |
4 |
9 |
≤12.1 |
5 |
≤37 |
18 |
4 |
≤12.1 |
4 |
1.0 |
getClientRects |
4 |
12 |
4 |
9 |
≤12.1 |
5 |
≤37 |
18 |
4 |
≤12.1 |
4 |
1.0 |
insertNode |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
intersectsNode |
1 |
17 |
17 |
No |
≤12.1 |
3 |
1 |
18 |
19 |
≤12.1 |
1 |
1.0 |
isPointInRange |
1 |
15 |
1 |
No |
≤12.1 |
3 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
selectNode |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
selectNodeContents |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
setEnd |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
setEndAfter |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
setEndBefore |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
setStart |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
setStartAfter |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
setStartBefore |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
startContainer |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
startOffset |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
surroundContents |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
toString |
1 |
12 |
1 |
9 |
9 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Range