The Range.setStart() method sets the start position of a Range.

If the startNode is a Node of type Text, Comment, or CDATASection, then startOffset is the number of characters from the start of startNode. For other Node types, startOffset is the number of child nodes between the start of the startNode.

Setting the start point below (lower in the document) the end point will result in a collapsed range with the start and end points both set to the specified start position.


range.setStart(startNode, startOffset);


The Node where the Range should start.
An integer greater than or equal to zero representing the offset for the start of the Range from the start of startNode.


var range = document.createRange();
var startNode = document.getElementsByTagName('p').item(2);
var startOffset = 0;
range.setStart(startNode, startOffset);

Get characters from a text node

This example uses the Range.setStart() and Range.setEnd() methods to define the contents of a range. The resulting range contains the first through fifth characters within a text node.


<p id="content">0123456789</p>
<p id="log"></p>


const element = document.getElementById('content');
const textNode = element.childNodes[0];
const range = document.createRange();
range.setStart(textNode, 0);  // Start at first character
range.setEnd(textNode, 5);    // End at fifth character
document.getElementById('log').textContent = range;



Specification Status Comment
The definition of 'Range.setStart()' in that specification.
Living Standard No change.
Document Object Model (DOM) Level 2 Traversal and Range Specification
The definition of 'Range.setStart()' in that specification.
Obsolete Initial specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setStartChrome 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  
Full support
Compatibility unknown  
Compatibility unknown

See also

Document Tags and Contributors

Last updated by: mfluehr,