selectionapi
Read, clear, and set text selection via the Selection API.
Import: @rtorcato/browser-common/selectionapi
๐ MDN: Selection
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
Selection | โ 1+ | โ 12+ | โ 1+ | โ 1.3+ | โ 1+ | Widely available |
Window.getSelection | โ 1+ | โ 12+ | โ 1+ | โ 1+ | โ 1+ | Widely available |
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Selection ยท Window.getSelection
Exampleโ
import {
isSelectionApiAvailable,
getSelectedText,
clearSelection,
selectElementText,
} from '@rtorcato/browser-common/selectionapi'
if (isSelectionApiAvailable()) {
const text = getSelectedText()
clearSelection()
selectElementText(document.querySelector('#code')!)
}
Exportsโ
isSelectionApiAvailable()โ feature checkgetSelection()โ the currentSelectionobject, ornullgetSelectedText()โ the currently selected text, or''clearSelection()โ clears the current selectionselectElementText(element)โ selects the text content of an element
See the API reference for full signatures.