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.
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().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.getRangeAt()Range object representing one of the ranges currently selected.Selection.collapse()Selection.extend()Selection.modify()
Selection.collapseToStart()Selection.collapseToEnd()Selection.selectAllChildren()Selection.addRange()Range object that will be added to the selection.Selection.removeRange()Selection.removeAllRanges()Selection.deleteFromDocument()Selection.toString()Selection.containsNode()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:
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).
Other key terms used in this section.
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. |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | 9 | (Yes) | (Yes) |
modify() | ? | ? | 4.0 (2) | ? | ? | ? |
| Feature | Android | Edge | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic support | ? | (Yes) | (Yes) | ? | ? | ? | ? |
modify() | ? | ? | 4.0 (2) | 1.0 | ? | ? | ? |
Window.getSelection, Document.getSelection, Range
selectionchange and selectstart
HTMLInputElement.setSelectionRange())Document.activeElement, HTMLElement.focus(), and HTMLElement.blur()
nsISelectionPrivate.dom/webidl/Selection.webidl
Selection.selectionLanguageChange() used to be exposed to the web content until Firefox 29
© 2005–2017 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