Selection API
suggest changeSyntax
const sel = window.getSelection();const sel = document.getSelection();which is the same call in a documentconst range = document.createRange();range.setStart(startNode, startOffset);range.setEnd(endNode, endOffset);
Parameters
| Parameter | Details |
|---|---|
| startOffset | If startNode is a Text node, the number of UTF-16 code units from the start of that node to where the range begins. Otherwise, the number of child nodes from the start of startNode. |
| endOffset | If endNode is a Text node, the number of UTF-16 code units from the start of that node to where the range ends. Otherwise, the number of child nodes from the start of endNode. |
Remarks
The Selection API allows you to view and change the elements and text that are selected (highlighted) in the document.
It is implemented as a singleton Selection instance that applies to the document, and holds a collection of Range objects, each representing one contiguous selected area.
Practically speaking, no browser except Mozilla Firefox supports multiple ranges in selections, and this is not encouraged by the spec either. Additionally, most users are not familiar with the concept of multiple ranges. As such, a developer can usually only concern themselves with one range.
Deselect everything that is selected
Select the contents of an element
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents