Range
The Range interface represents a fragment of a document that can contain nodes and parts of text nodes.
Extends
Methods
cloneContents()
cloneContents(): DocumentFragment;
The cloneContents() method of the Range interface copies the selected Node children of the range's commonAncestorContainer and puts them in a new DocumentFragment object.
Returns
cloneRange()
cloneRange(): Range;
The Range.cloneRange() method returns a Range object with boundary points identical to the cloned Range.
Returns
Range
collapse()
collapse(toStart?): void;
The collapse() method of the Range interface collapses the Range to one of its boundary points.
Parameters
| Parameter | Type |
|---|---|
toStart? | boolean |
Returns
void
compareBoundaryPoints()
compareBoundaryPoints(how, sourceRange): number;
The compareBoundaryPoints() method of the Range interface compares the boundary points of the Range with those of another range.
Parameters
| Parameter | Type |
|---|---|
how | number |
sourceRange | Range |
Returns
number
comparePoint()
comparePoint(node, offset): number;
The comparePoint() method of the Range interface determines whether a specified point is before, within, or after the Range. The point is specified by a reference node and an offset within that node.
Parameters
| Parameter | Type |
|---|---|
node | Node |
offset | number |
Returns
number
createContextualFragment()
createContextualFragment(string): DocumentFragment;
The Range.createContextualFragment() method of the Range interface returns a DocumentFragment representing the parsed input HTML or XML.
Parameters
| Parameter | Type |
|---|---|
string | string |
Returns
deleteContents()
deleteContents(): void;
The Range.deleteContents() method removes all completely-selected nodes within this range from the document. For the partially selected nodes at the start or end of the range, only the selected portion of the text is deleted, while the node itself remains intact. Afterwards, the range is collapsed to the end of the last selected node.
Returns
void
detach()
detach(): void;
The Range.detach() method does nothing. It used to disable the Range object and enable the browser to release associated resources. The method has been kept for compatibility.
Returns
void
Deprecated
extractContents()
extractContents(): DocumentFragment;
The extractContents() method of the Range interface is similar to a combination of Range.cloneContents() and Range.deleteContents(). It removes the child Nodes of the range from the document, clones them, and returns them as a new DocumentFragment object. For partially selected nodes, only the selected text is deleted, but all containing parent nodes up to the common ancestor are cloned as well, resulting in two copies of these nodes, one in the original document and one in the extracted fragment.
Returns
getBoundingClientRect()
getBoundingClientRect(): DOMRect;
The Range.getBoundingClientRect() method returns a DOMRect object that bounds the contents of the range; this is a rectangle enclosing the union of the bounding rectangles for all the elements in the range.
Returns
getClientRects()
getClientRects(): DOMRectList;
The Range.getClientRects() method returns a list of DOMRect objects representing the area of the screen occupied by the range. This is created by aggregating the results of calls to Element.getClientRects() for all the elements in the range.
Returns
insertNode()
insertNode(node): void;
The Range.insertNode() method inserts a node at the start of the Range.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
intersectsNode()
intersectsNode(node): boolean;
The Range.intersectsNode() method returns a boolean indicating whether the given Node intersects the Range.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
boolean
isPointInRange()
isPointInRange(node, offset): boolean;
The isPointInRange() method of the Range interface determines whether a specified point is within the Range. The point is specified by a reference node and an offset within that node. It is equivalent to calling Range.comparePoint() and checking if the result is 0.
Parameters
| Parameter | Type |
|---|---|
node | Node |
offset | number |
Returns
boolean
selectNode()
selectNode(node): void;
The Range.selectNode() method sets the Range to contain the Node and its contents. The parent Node of the start and end of the Range will be the same as the parent of the referenceNode.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
selectNodeContents()
selectNodeContents(node): void;
The Range.selectNodeContents() method sets the Range to contain the contents of a Node.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
setEnd()
setEnd(node, offset): void;
The Range.setEnd() method sets the end position of a Range to be located at the given offset into the specified node. Setting the end point above (higher in the document) than the start point will result in a collapsed range with the start and end points both set to the specified end position.
Parameters
| Parameter | Type |
|---|---|
node | Node |
offset | number |
Returns
void
setEndAfter()
setEndAfter(node): void;
The Range.setEndAfter() method sets the end position of a Range relative to another Node. The parent Node of end of the Range will be the same as that for the referenceNode.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
setEndBefore()
setEndBefore(node): void;
The Range.setEndBefore() method sets the end position of a Range relative to another Node. The parent Node of end of the Range will be the same as that for the referenceNode.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
setStart()
setStart(node, offset): void;
The Range.setStart() method sets the start position of a Range.
Parameters
| Parameter | Type |
|---|---|
node | Node |
offset | number |
Returns
void
setStartAfter()
setStartAfter(node): void;
The Range.setStartAfter() method sets the start position of a Range relative to a Node. The parent Node of the start of the Range will be the same as that for the referenceNode.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
setStartBefore()
setStartBefore(node): void;
The Range.setStartBefore() method sets the start position of a Range relative to another Node. The parent Node of the start of the Range will be the same as that for the referenceNode.
Parameters
| Parameter | Type |
|---|---|
node | Node |
Returns
void
surroundContents()
surroundContents(newParent): void;
The surroundContents() method of the Range interface surrounds the selected content by a provided node. It extracts the contents of the range, replaces the children of newParent with the extracted contents, inserts newParent at the location of the extracted contents, and makes the range select newParent.
Parameters
| Parameter | Type |
|---|---|
newParent | Node |
Returns
void
toString()
toString(): string;
Returns a string representation of an object.
Returns
string
Properties
collapsed
readonly collapsed: boolean;
The read-only collapsed property of the AbstractRange interface returns true if the range's start position and end position are the same.
Inherited from
commonAncestorContainer
readonly commonAncestorContainer: Node;
The Range.commonAncestorContainer read-only property returns the deepest — or furthest down the document tree — Node that contains both boundary points of the Range. This means that if startContainer and endContainer both refer to the same node, this node is the common ancestor container.
END_TO_END
readonly END_TO_END: 2;
END_TO_START
readonly END_TO_START: 3;
endContainer
readonly endContainer: Node;
The read-only endContainer property of the AbstractRange interface returns the Node in which the end of the range is located.
Inherited from
endOffset
readonly endOffset: number;
The endOffset property of the AbstractRange interface returns the offset into the end node of the range's end position.
Inherited from
START_TO_END
readonly START_TO_END: 1;
START_TO_START
readonly START_TO_START: 0;
startContainer
readonly startContainer: Node;
The read-only startContainer property of the AbstractRange interface returns the Node in which the start of the range is located.
Inherited from
startOffset
readonly startOffset: number;
The read-only startOffset property of the AbstractRange interface returns the offset into the start node of the range's start position.