This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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.
Selection.anchorNode
Read only
Node
in which the selection begins.Selection.anchorOffset
Read only
Selection.focusNode
Read only
Node
in which the selection ends.Selection.focusOffset
Read only
Selection.isCollapsed
Read only
Selection.rangeCount
Read only
Selection.type
Read only
DOMString
describing the type of the current selection.Selection.addRange()
Range
object that will be added to the selection.Selection.collapse()
Selection.collapseToEnd()
Selection.collapseToStart()
Selection.containsNode()
Selection.deleteFromDocument()
Selection.empty()
removeAllRanges()
— See Selection.removeAllRanges()
for more details.Selection.extend()
Selection.getRangeAt()
Range
object representing one of the ranges currently selected.Selection.modify()
Selection.removeRange()
Selection.removeAllRanges()
Selection.selectAllChildren()
Selection.setBaseAndExtent()
Selection.setPosition()
collapse()
— See Selection.collapse()
for more details.Selection.toString()
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.
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 objectrange
is a Range
objectAs 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 (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).
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:
Selection.collapse()
Selection.collapseToStart()
Selection.collapseToEnd()
Selection.extend()
Selection.selectAllChildren()
Selection.addRange()
Selection.setBaseAndExtent()
and when the range is modified using the following methods:
Range.setStart()
Range.setEnd()
Range.setStartBefore()
Range.setStartAfter()
Range.setEndBefore()
Range.setEndAfter()
Range.collapse()
Range.selectNode()
Range.selectNodeContents()
Other key terms used in this section.
contenteditable
set on it, or the HTML child of a document that has designMode
enabled.document.activeElement
.range
object. Range objects can also be created via the DOM and programmatically added or removed from a selection.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. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | Yes
|
9 | Yes | Yes |
addRange
|
1 | Yes | Yes | ? | Yes | ? |
anchorNode
|
1 | Yes | Yes | ? | Yes | ? |
anchorOffset
|
1 | Yes | Yes | ? | Yes | ? |
collapse
|
Yes | Yes | Yes | 9 | Yes | Yes |
collapseToStart
|
Yes | Yes | Yes | ? | Yes | ? |
collapseToEnd
|
Yes | 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 | ? | Yes | ? |
getRangeAt
|
Yes | Yes | Yes | ? | Yes | ? |
isCollapsed
|
1 | Yes | 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
|
1 | Yes | 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 | Yes | Yes | Yes | ? | ? |
anchorNode
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
anchorOffset
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
collapse
|
Yes | Yes | Yes | Yes | Yes | Yes | ? |
collapseToStart
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
collapseToEnd
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
containsNode
|
Yes | 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 | Yes | Yes | ? | ? |
focusOffset
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
getRangeAt
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
isCollapsed
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
modify
|
Yes | Yes | ? | 4 | Yes | Yes | ? |
rangeCount
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
removeRange
|
58 | 58 | Yes | Yes | 45 | No | ? |
removeAllRanges
|
Yes | Yes | Yes | Yes | Yes | Yes | ? |
selectAllChildren
|
Yes | Yes | Yes | Yes | Yes | Yes | ? |
setBaseAndExtent
|
Yes | Yes | ? | 53 | Yes | ? | ? |
setPosition() as alias of collapse()
|
Yes | Yes | ? | 55 | Yes | Yes | ? |
toString
|
Yes | Yes | Yes | Yes | Yes | ? | ? |
type
|
Yes | Yes | Yes | 57 | Yes | ? | ? |
Window.getSelection
, Document.getSelection
, Range
selectionchange
and selectstart
HTMLInputElement.setSelectionRange()
)Document.activeElement
, HTMLElement.focus()
, and HTMLElement.blur()
nsISelectionPrivate
.dom/webidl/Selection.webidl
Selection.selectionLanguageChange()
Obsolete since Gecko 29 used to be exposed to the web content until Firefox 29
© 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