The Range.setEnd() method sets the end position of a Range to be located at the given offset into the specified node x.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.
Syntax
range.setEnd(endNode, endOffset);
Parameters
endNode- The
Nodeinside which theRangeshould end. endOffset- An integer greater than or equal to zero representing the offset for the end of the
Rangefrom the start ofendNode.
Return value
undefined.
Exceptions
Exceptions are thrown as DOMException objects of the following types:
InvalidNodeTypeError- The node specified by
endNodeis a doctype node; range endpoints cannot be located inside a doctype node. IndexSizeError- The value specified by
endOffsetis either greater than or equal to the length of the node or is less than zero.
Usage notes
If the endNode is a Node of type Text, Comment, or CDataSection, then endOffset is the number of characters from the start of endNode. For other Node types, endOffset is the number of child nodes between the start of the endNode.
Example
const range = document.createRange();
const endNode = document.getElementsByTagName('p').item(3);
const endOffset = endNode.childNodes.length;
range.setEnd(endNode, endOffset);
setEnd() is commonly used in conjunction with setStart() to fully configure a range.
Specifications
| Specification | Status | Comment |
|---|---|---|
| DOM The definition of 'Range.setEnd()' in that specification. |
Living Standard | No change. |
| Document Object Model (DOM) Level 2 Traversal and Range Specification The definition of 'Range.setEnd()' in that specification. |
Obsolete | Initial specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
setEnd | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 4 | IE Full support 9 | Opera Full support 9 | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
