The Selection API provides functionality for reading and manipulating the range of text selected by the user.
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
Window.getSelection()
, Document.getSelection()
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
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
selectionchange
event is fired on the current object (i.e. when the selected text range changes).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. |
Selection
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes
|
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
|
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 | 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
|
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 | ? | ? |
Range
object.
© 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