Use document.getSelection()
to retrieve the Selection object representing the range of text selected by the user or the current position of the caret.
var selection = document.getSelection();
A quick reference guide to the HTML DOM Selection API, covering essential methods, properties, and event handling for managing text selections in web applications.
Use
|
|
The node in which the selection begins. |
|
The offset into the |
|
The node in which the selection ends. |
|
The offset into the |
|
A boolean indicating whether the selection’s start and end points are at the same position. |
|
The number of ranges in the selection. |
|
|
Adds a |
|
Removes a |
|
Removes all ranges from the selection, effectively deselecting everything. |
|
Example: Selecting part of a text node.
|
|
Collapses the selection to a single point at the specified |
|
Collapses the selection to its start point. |
|
Collapses the selection to its end point. |
|
Example: Delete selected text when a button is clicked.
|
Use the
|