Selection API
Note:This API isnot available inWeb Workers (not exposed viaWorkerNavigator).
TheSelection API enables developers to access and manipulate the portion of a document selected by the user.
TheWindow.getSelection() andDocument.getSelection() methods return aSelection object representing the portion of the document selected by the user. ASelection object provides methods to:
- access the currently selected nodes
- modify the current selection, expanding or collapsing it or selecting an entirely different part of the document
- delete parts of the current selection from the DOM.
The Selection API also provides two events, both firing onDocument:
- the
selectstartevent is fired when the user starts to make a new selection - the
selectionchangeevent is fired when the current selection changes.
In this article
Interfaces
SelectionAn interface which represents the part of the document selected by the user or the current position of the caret.
Document.getSelection()A method returning a
Selectionobject representing the current selection or current position of the caret.Window.getSelection()A method returning a
Selectionobject representing the current selection or current position of the caret.Document.selectionchangeAn event which is fired when the current selection is changed.
Node.selectstartAn event which is fired when a user starts a new selection.
Specifications
| Specification |
|---|
| Selection API> # selection-interface> |