Range.collapse()

The Range.collapse() method collapses the Range to one of its boundary points.

A collapsed Range is empty, containing no content, specifying a single-point in a DOM tree. To determine if a Range is already collapsed, see the Range.collapsed property.

Syntax

range.collapse(toStart);

Parameters

toStart Optional
A Boolean value: true collapses the Range to its start, false to its end. If omitted, it defaults to false .

Example

var range = document.createRange();

referenceNode = document.getElementsByTagName("div").item(0);
range.selectNode(referenceNode);
range.collapse(true);

Specifications

Specification Status Comment
DOM
The definition of 'Range.collapse()' in that specification.
Living Standard The parameter is now optional and default to false.
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Range.collapse()' in that specification.
Obsolete Initial specification.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
collapseChrome Full support YesEdge Full support 12Firefox Full support 4IE Full support 9Opera Full support 9Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
toStart parameter optional
Experimental
Chrome Full support YesEdge Full support ≤79Firefox Full support 25IE No support No
Notes
No support No
Notes
Notes Optional but defaults to true.
Opera Full support 15Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 25Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

See also