W3cubDocs

/DOM

Selection API

The Selection API provides functionality for reading and manipulating the range of text selected by the user.

Concepts and usage

To retrieve the current text range the user has selected, you can use the Window.getSelection() or Document.getSelection() method, storing the return value — a Selection object — in a variable for futher use.

Once your selection is in a variable, you perform a variety of operations on it, for example copying the selection to a text string using Selection.toString(), adding a range (as represented by a standard Range object) to the selection (or removing one) with Selection.addRange()/Selection.removeRange(), or changing the selection to be the entire contents of a DOM node using Selection.selectAllChildren().

You can run code in response to the selection being changed, or a new selection being started, using the GlobalEventHandlers.onselectionchange and GlobalEventHandlers.onselectstart event handlers.

Selection API interfaces

Selection
Represents the range of text selected by the user or the current position of the caret.

Extensions to other interfaces

Window.getSelection(), Document.getSelection()
Returns a Selection object representing the range of text selected by the user or the current position of the caret. Document.getSelection() is basically an alias of Window.getSelection().
GlobalEventHandlers.onselectstart
Represents the event handler that is called when a selectstart event is fired on the current object (i.e. when a new range of text is about to be selected by the user).
GlobalEventHandlers.onselectionchange
Represents the event handler that is called when a selectionchange event is fired on the current object (i.e. when the selected text range changes).

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, which is now outdated.

Browser compatibilityUpdate compatibility data on GitHub

Selection

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support Yes Yes Yes
Yes
The GlobalEventHandlers.onselectionchange and GlobalEventHandlers.onselectstart event handlers are supported as of Firefox 52.
9 Yes Yes
addRange ? Yes Yes ? ? ?
anchorNode ? Yes Yes ? ? ?
anchorOffset ? Yes Yes ? ? ?
collapse Yes Yes Yes 9 Yes Yes
collapseToStart Yes Yes Yes ? ? ?
collapseToEnd Yes Yes Yes ? ? ?
containsNode Yes Yes 4
4
Before Firefox 35, the method didn't throw if node was null.
No Yes Yes
deleteFromDocument Yes Yes 55 ? Yes ?
empty() as alias of removeAllRanges() Yes ? 55 ? Yes Yes
extend Yes Yes Yes No Yes Yes
focusNode Yes Yes 3.6 10 Yes 5.1
focusOffset Yes Yes Yes ? ? ?
getRangeAt Yes Yes Yes ? ? ?
isCollapsed ? Yes Yes ? ? ?
modify Yes ? 4 ? Yes Yes
rangeCount Yes Yes Yes Yes Yes ?
removeRange 58 Yes Yes ? 45 No
removeAllRanges Yes Yes Yes Yes Yes Yes
selectAllChildren Yes Yes Yes Yes Yes Yes
setBaseAndExtent Yes ? 53 ? Yes ?
setPosition() as alias of collapse() Yes ? 55 ? Yes Yes
toString ? Yes Yes ? ? ?
type Yes Yes 57 Yes Yes ?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes Yes Yes
Yes
The GlobalEventHandlers.onselectionchange and GlobalEventHandlers.onselectstart event handlers are supported as of Firefox 52.
Yes Yes ?
addRange ? ? Yes Yes ? ? ?
anchorNode ? ? Yes Yes ? ? ?
anchorOffset ? ? Yes Yes ? ? ?
collapse ? Yes Yes Yes Yes Yes ?
collapseToStart ? Yes Yes Yes ? ? ?
collapseToEnd ? Yes Yes Yes ? ? ?
containsNode ? Yes Yes 4
4
Before Firefox 35, the method didn't throw if node was null.
Yes Yes ?
deleteFromDocument Yes Yes Yes 55 Yes ? ?
empty() as alias of removeAllRanges() Yes Yes ? 55 Yes Yes ?
extend No Yes Yes Yes Yes Yes ?
focusNode ? Yes Yes Yes ? ? ?
focusOffset ? Yes Yes Yes ? ? ?
getRangeAt ? Yes Yes Yes ? ? ?
isCollapsed ? ? Yes Yes ? ? ?
modify Yes Yes ? 4 Yes Yes ?
rangeCount ? Yes Yes Yes ? ? ?
removeRange 58 58 Yes Yes 45 No ?
removeAllRanges ? Yes Yes Yes Yes Yes ?
selectAllChildren ? Yes Yes Yes Yes Yes ?
setBaseAndExtent Yes Yes ? 53 Yes ? ?
setPosition() as alias of collapse() Yes Yes ? 55 Yes Yes ?
toString ? ? Yes Yes ? ? ?
type Yes Yes Yes 57 Yes ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Selection_API