Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Selection object

Represents the active selection, which is typically a highlighted block of text or other elements in the document. The active selection can be used to complete an action issued by a user or a script.

HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 7.6.1Internet Explorer 9



The Selection object has these types of members:


The Selection object has these methods.


Adds a Range to the current selection.

collapse (setPosition)

Replaces the current selection with an empty selection (or a caret) at the given offset.


Collapses or sets the insertion point or caret at the end of a selection object.


Collapses, or sets the insertion point or caret at the beginning of a selection object.


Determines if the given node is included in the selection.


Deletes the selected nodes from a document.


Removes all ranges of the selection.


Moves the focus of the selection to a specified offset within the given node.


Returns a specified Range from a selection. The Range is specified by an index and cannot be greater than the number that is returned by rangeCount.


Removes all ranges from a selection.


Removes a range from a selection.


Replaces the current selection with all the contents of the given node.


Sets the anchor (base) and focus (extent) boundary points for the selection.



The Selection object has these properties.


anchorNode (baseNode)

Returns the element or node that contains the start of the selection.

anchorOffset (baseOffset)

Retrieves the starting position of a selection that is relative to the anchorNode / baseNode.

focusNode / extentNode

Retrieves the element or node that contains the end of a selection.

focusOffset (extentOffset)

Retrieves the end position of a selection that is relative to the focusNode / extentNode.


Retrieves whether a selection is collapsed or empty.


Returns the number of ranges in a selection


Standards information


You can use the Selection object as input from the user to identify which portion of the document to act on, or as output to the user to show the results of an action.

Users and scripts can create selections. Users create selections by dragging a finger or mouse over a portion of the document. Scripts create selections by calls to selection methods. To get the active selection, apply the getSelection method to the document object.

A document can have only one selection at a time. Use the isCollapsed property to test whether a selection is empty. Although an empty selection contains nothing, you can use it to mark a position, or place a caret in the document.



© 2017 Microsoft