coderef

SelectionBaseline · Widely available

The Selection API controls and modifies user text selections within the page.

Supported in all major browsers for at least 30 months — safe to use without fallbacks. Baseline since Mar 7, 2017. Widely available since Sep 7, 2019.

Browser support

DesktopChrome
1–10Not supported
11+Supportedsince Apr 27, 2011
Current: 155
Edge
12+Supportedsince Jul 29, 2015
Current: 154
Firefox
1–51Not supported
52+Supportedsince Mar 7, 2017
Current: 157
Safari
1–5Not supported
5.1+Supportedsince Jul 20, 2011
Current: 27
MobileChrome Android
18+Supportedsince Jun 27, 2012
Current: 155
Firefox Android
4–51Not supported
52+Supportedsince Mar 7, 2017
Current: 157
Safari iOS
1–4.2Not supported
5+Supportedsince Oct 12, 2011
Current: 27

How to use

This feature is Baseline widely available — use it freely, no fallback needed.

Example

Full syntax and examples are on MDN: AbstractRange.

Feature detection

JS · check before using
if ("Selection" in globalThis) {
  // Selection is available
} else {
  // load a polyfill or use a fallback
}

Sub-features

Support for each part of the feature (79). The overall table above shows when the whole feature works.

Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
AbstractRange90906914.1907914.5
AbstractRange.collapsed90906914.1907914.5
AbstractRange.endContainer90906914.1907914.5
AbstractRange.endOffset90906914.1907914.5
AbstractRange.startContainer90906914.1907914.5
AbstractRange.startOffset90906914.1907914.5
Document.createRange112111841
Document.getSelection21257418573.2
Document.selectionchange_event1112525.118525
HTMLInputElement.selectionchange_event12712792181279218
HTMLTextAreaElement.selectionchange_event12712792181279218
Node.selectstart_event112521.31852✕
Range112111841
Range.Range291524829248
Range.cloneContents112111841
Range.cloneRange112111841
Range.collapse112111841
Range.collapse.toStart_parameter_optional11625118251
Range.collapsed112111841
Range.commonAncestorContainer112111841
Show 59 more
Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
Range.compareBoundaryPoints112111841
Range.comparePoint117131841
Range.createContextualFragment112111841
Range.deleteContents112111841
Range.endContainer112111841
Range.endOffset112111841
Range.extractContents112111841
Range.getBoundingClientRect412451844
Range.getClientRects412451844
Range.insertNode112111841
Range.intersectsNode11717318191
Range.isPointInRange115131841
Range.selectNode112111841
Range.selectNodeContents112111841
Range.setEnd112111841
Range.setEndAfter112111841
Range.setEndBefore112111841
Range.setStart112111841
Range.setStartAfter112111841
Range.setStartBefore112111841
Range.startContainer112111841
Range.startOffset112111841
Range.surroundContents112111841
Range.toString112111841
Selection11211.31841
Selection.addRange112131841
Selection.anchorNode11211.31841
Selection.anchorOffset11211.31841
Selection.collapse11211.31841
Selection.collapse.offset_parameter_optional117551.318551
Selection.collapseToEnd11211.31841
Selection.collapseToStart11211.31841
Selection.containsNode11213.11842
Selection.deleteFromDocument11213.11842
Selection.direction1371371261713712617
Selection.empty112551.318551
Selection.extend11213.11842
Selection.extend.offset_parameter_optional117553.118552
Selection.focusNode11211.31841
Selection.focusOffset11211.31841
Selection.getRangeAt112131841
Selection.isCollapsed11211.31841
Selection.modify17941.31841
Selection.rangeCount112131841
Selection.removeAllRanges112131841
Selection.removeRange581211758417
Selection.selectAllChildren11213.11842
Selection.setBaseAndExtent112531.318531
Selection.setPosition114551.318551
Selection.toString11211.31841
Selection.type112571.318571
StaticRange60186910.1607910.3
StaticRange.StaticRange90907113.1907913.4
StaticRange.collapsed60186910.1607910.3
StaticRange.endContainer60186910.1607910.3
StaticRange.endOffset60186910.1607910.3
StaticRange.startContainer60186910.1607910.3
StaticRange.startOffset60186910.1607910.3
Window.getSelection112111841