Selection

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

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().

A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection and the focus is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button and the focus is placed where you released the mouse button. Anchor and focus should not be confused with the start and end positions of a selection, since anchor can be placed before the focus or vice versa, depending on the direction you made your selection.

Properties

Selection.anchorNode Read only
Returns the Node in which the selection begins.
Selection.anchorOffset Read only
Returns 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.
Selection.focusNode Read only
Returns the Node in which the selection ends.
Selection.focusOffset Read only
Returns 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.
Selection.isCollapsed Read only
Returns a Boolean indicating whether the selection's start and end points are at the same position.
Selection.rangeCount Read only
Returns the number of ranges in the selection.
Selection.type Read only
Returns a DOMString describing the type of the current selection.

Methods

Selection.addRange()
A Range object that will be added to the selection.
Selection.collapse()
Collapses the current selection to a single point.
Selection.collapseToEnd()
Collapses the selection to the end of the last range in the selection.
Selection.collapseToStart()
Collapses the selection to the start of the first range in the selection.
Selection.containsNode()
Indicates if a certain node is part of the selection.
Selection.deleteFromDocument()
Deletes the selection's content from the document.
Selection.empty()
Removes all ranges from the selection. This is an alias for removeAllRanges() — See Selection.removeAllRanges() for more details.
Selection.extend()
Moves the focus of the selection to a specified point.
Selection.getRangeAt()
Returns a Range object representing one of the ranges currently selected.
Selection.modify()
Changes the current selection.
Selection.removeRange()
Removes a range from the selection.
Selection.removeAllRanges()
Removes all ranges from the selection.
Selection.selectAllChildren()
Adds all the children of the specified node to the selection.
Selection.setBaseAndExtent()
Sets the selection to be a range including all or parts of two specified DOM nodes, and any content located between them.
Selection.setPosition()
Collapses the current selection to a single point. This is an alias for collapse() — See Selection.collapse() for more details.
Selection.toString()
Returns a string currently being represented by the selection object, i.e. the currently selected text.

Notes

String representation of a selection

Calling the Selection.toString() method returns the text contained in the selection, e.g.:

var selObj = window.getSelection();
window.alert(selObj); 

Note that using a selection object as the argument to window.alert will call the object's toString method.

Multiple ranges in a selection

A selection object represents the ranges that the user has selected. Typically, it holds only one range, accessed as follows:

var selObj = window.getSelection();
var range  = selObj.getRangeAt(0);
  • selObj is a Selection object
  • range is a Range object

As the Selection API specification notes, the Selection API was initially created by Netscape and used multiple ranges, for instance, to allow the user to select a column from a <table>. However browsers other than Gecko did not implement multiple ranges, and the specification also requires the selection to always have a single range.

Selection and input focus

Selection and input focus (indicated by Document.activeElement) have a complex relation, that depends on the browser. In cross-browser compatible code it's better to handle them separately.

Safari and Chrome (unlike Firefox) historically focus the element containing selection when modifying the selection programmatically, but this might change in the future (see W3C bug 14383 and WebKit bug 38696).

Behavior of Selection API in terms of editing host focus changes

The Selection API has a common behaviour (i.e. shared between browsers) that govern how focus behaviour changes for editing hosts, after some of its methods are called.

The behaviours is that an editing host gains focus if the previous selection was outside of it, but then a Selection API method is called that causes a new selection to be made with a selection range inside the editing host. Focus then moves to the editing host.

Note: The Selection API methods may move focus only to an editing host, not to other focusable elements (e.g. <a>).

The above behaviour applies to selections made using the following methods:

and when the range is modified using the following methods:

Glossary

Other key terms used in this section.

anchor
The anchor of a selection is the beginning point of the selection. When making a selection with a mouse, the anchor is where in the document the mouse button is initially pressed. As the user changes the selection using the mouse or the keyboard, the anchor does not move.
Editing host
An editable element — i.e. an HTML element with contenteditable set on it, or the HTML child of a document that has designMode enabled.
focus of a selection
The focus of a selection is the end point of the selection. When making a selection with a mouse, the focus is where in the document the mouse button is released. As the user changes the selection using the mouse or the keyboard, the focus is the end of the selection that moves. Note: This is not the same as the focused element of the document, as returned by document.activeElement.
range
A range is a contiguous part of a document. A range can contain entire nodes as well as portions of nodes, such as a portion of a text node. A user will normally only select a single range at a time, but it's possible for a user to select multiple ranges (e.g. by using the Control key). A range can be retrieved from a selection as a range object. Range objects can also be created via the DOM and programmatically added or removed from a selection.

Specifications

Specification Status Comment
Selection API
The definition of 'Selection' in that specification.
Working Draft The Selection API specification is based on the HTML Editing APIs specification and focuses on the Selection-related functionality.
HTML Editing APIs
The definition of 'Selection' in that specification.
Editor's Draft Initial (older) definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) (Yes)
52 (52)[1]
9 (Yes) (Yes)
modify() (Yes) ? 4.0 (2) ? ? (Yes)
setBaseAndExtent() ? ? 53 (53) ? ? ?
deleteFromDocument() ? (Yes) 55 (55) ? ? ?
empty() as alias of removeAllRanges() (Yes) ? 55 (55) ? (Yes) (Yes)
setPosition() as alias of collapse() (Yes) ? 55 (55) ? (Yes) (Yes)
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? (Yes)

(Yes)
52.0 (52)[1]

? ? (Yes)
modify() ? ? 4.0 (2) ? ? (Yes)
setBaseAndExtent() ? ? 53.0 (53) ? ? ?
deleteFromDocument() ? (Yes) 55.0 (55) ? ? ?
empty() as alias of removeAllRanges() ? ? 55.0 (55) ? (Yes) (Yes)
setPosition() as alias of collapse() ? ? 55.0 (55) ? (Yes) (Yes)

[1] The GlobalEventHandlers.onselectionchange and GlobalEventHandlers.onselectstart event handlers are suported as of Firefox 52.

See also

Gecko notes