Selection
A Selection object represents the range of text selected by the user or the current position of the caret. To obtain a Selection object for examination or modification, call window.getSelection().
Documentation Selection by Mozilla Contributors, licensed under CC-BY-SA 2.5.
Instance Members
anchorNode: NodeReturns the Node in which the selection begins.
anchorOffset: IntReturns a number representing the offset of the selection's anchor within the anchorNode. If anchorNode is a text node, this is the number of characters within anchorNode preceding the anchor. If anchorNode is an element, this is the number of child nodes of the anchorNode preceding the anchor.
focusNode: NodeReturns the Node in which the selection ends.
focusOffset: IntReturns a number representing the offset of the selection's anchor within the focusNode. If focusNode is a text node, this is the number of characters within focusNode preceding the focus. If focusNode is an element, this is the number of child nodes of the focusNode preceding the focus.
isCollapsed: BoolReturns a Boolean indicating whether the selection's start and end points are at the same position.
rangeCount: IntReturns the number of ranges in the selection.
type: StringReturns a DOMString describing the type of the current selection.
caretBidiLevel: IntReturns a Range object representing one of the ranges currently selected.
| Name | Type |
|---|---|
index |
Int |
| Returns |
|---|
| Range |
A Range object that will be added to the selection.
| Name | Type |
|---|---|
range |
Range |
Removes a range from the selection.
| Name | Type |
|---|---|
range |
Range |
removeAllRanges(): VoidRemoves all ranges from the selection.
empty(): VoidCollapses the current selection to a single point.
| Name | Type | Default |
|---|---|---|
node |
Node | |
offset |
Int | 0 |
| Name | Type | Default |
|---|---|---|
node |
Node | |
offset |
Int | 0 |
collapseToStart(): VoidCollapses the selection to the start of the first range in the selection.
collapseToEnd(): VoidCollapses the selection to the end of the last range in the selection.
Moves the focus of the selection to a specified point.
| Name | Type | Default |
|---|---|---|
node |
Node | |
offset |
Int | 0 |
Sets the selection to be a range including all or parts of two specified DOM nodes, and any content located between them.
| Name | Type |
|---|---|
anchorNode |
Node |
anchorOffset |
Int |
focusNode |
Node |
focusOffset |
Int |
Adds all the children of the specified node to the selection.
| Name | Type |
|---|---|
node |
Node |
deleteFromDocument(): VoidDeletes the selection's content from the document.
Indicates if a certain node is part of the selection.
| Name | Type | Default |
|---|---|---|
node |
Node | |
allowPartialContainment |
Bool | false |
| Returns |
|---|
| Bool |
Changes the current selection.
| Name | Type |
|---|---|
alter |
String |
direction |
String |
granularity |
String |