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 compatibility

Selection

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support Yes
Notes
Full support Yes
Notes
Notes The GlobalEventHandlers.onselectionchange and GlobalEventHandlers.onselectstart event handlers are supported as of Firefox 52.
IE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes The GlobalEventHandlers.onselectionchange and GlobalEventHandlers.onselectstart event handlers are supported as of Firefox 52.
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
addRange
Experimental
Chrome Full support 1Edge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
anchorNode
Experimental
Chrome Full support 1Edge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
anchorOffset
Experimental
Chrome Full support 1Edge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
collapse
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE Full support 9Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
collapseToStart
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
collapseToEnd
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
containsNode
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support 4
Notes
Full support 4
Notes
Notes Before Firefox 35, the method didn't throw if node was null.
IE No support NoOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4
Notes
Full support 4
Notes
Notes Before Firefox 35, the method didn't throw if node was null.
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
deleteFromDocument
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support 55IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 55Opera Android Full support YesSafari iOS ? Samsung Internet Android ?
empty() as alias of removeAllRanges()
Experimental
Chrome Full support YesEdge ? Firefox Full support 55IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 55Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
extend
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE No support NoOpera Full support YesSafari Full support YesWebView Android No support NoChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
focusNode
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support 3.6IE Full support 10Opera Full support YesSafari Full support 5.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
focusOffset
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
getRangeAt
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
isCollapsed
Experimental
Chrome Full support 1Edge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
modify
ExperimentalNon-standard
Chrome Full support YesEdge ? Firefox Full support 4IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
rangeCount
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
removeRange
Experimental
Chrome Full support 58Edge Full support YesFirefox Full support YesIE ? Opera Full support 45Safari No support NoWebView Android Full support 58Chrome Android Full support 58Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support 45Safari iOS No support NoSamsung Internet Android ?
removeAllRanges
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
selectAllChildren
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
setBaseAndExtent
Experimental
Chrome Full support YesEdge ? Firefox Full support 53IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 53Opera Android Full support YesSafari iOS ? Samsung Internet Android ?
setPosition() as alias of collapse()
Experimental
Chrome Full support YesEdge ? Firefox Full support 55IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 55Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
toString
Experimental
Chrome Full support 1Edge Full support YesFirefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android ?
type
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support 57IE Full support YesOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 57Opera Android Full support YesSafari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
See implementation notes.
See implementation notes.

See also

Document Tags and Contributors

Contributors to this page: d4rkne55, fscholz, chrisdavidmills
Last updated by: d4rkne55,