Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Range — a live range in the DOM tree

Range describes a DOM fragment between two boundaries. It supports extracting, deleting, and inserting content, while its boundaries automatically respond to tree changes.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const range = document.createRange();
range.setStart(node, startOffset);
range.setEnd(node, endOffset);
range.extractContents();
range.insertNode(node);

Range is a live range defined by two node-and-offset pairs. Create it with document.createRange() or new Range(). It is a DOM-standard Web API, not a feature of a particular ECMAScript edition; Node 24 in a repository does not establish availability. The example requires a browser DOM.

Range.startOffset and Range.endOffset count UTF-16 code units in CharacterData, but child nodes, not characters, in elements. Range.commonAncestorContainer returns the deepest shared node containing both boundaries; this can be the text node itself. Range.collapsed means identical boundaries, not merely empty text.

Range.extractContents() removes content and returns a DocumentFragment: fully contained nodes move, while partially contained structures are copied as needed. Range.deleteContents() removes without returning a fragment. Both operations collapse the range. Range.insertNode(node) inserts at the start rather than replacing content; it can split a text node and expand a collapsed range.

Range.setStartAfter(node) places the start after the node in its parent; the node must have a parent. Range.collapse(true) collapses to the start, whereas Range.collapse(false) or Range.collapse() collapses to the end. Range.cloneRange() creates a separate live range with the same boundaries, not a DOM copy or immutable snapshot: later mutations can affect both ranges.

Examples

(() => {
  const host = document.createElement('div');
  host.textContent = 'ABCDE';
  const range = document.createRange();
  range.setStart(host.firstChild, 1);
  range.setEnd(host.firstChild, 4);
  console.log(range.commonAncestorContainer === host.firstChild);
  console.log(range.startOffset, range.endOffset, range.collapsed);

  const copy = range.cloneRange();
  copy.collapse(true);
  console.log(copy.collapsed, range.collapsed);

  const fragment = range.extractContents();
  console.log(fragment.textContent, host.textContent, range.collapsed);
  const mark = document.createElement('mark');
  mark.appendChild(fragment);
  range.insertNode(mark);
  range.setStartAfter(mark);
  range.collapse(true);
  console.log(host.textContent, range.startOffset, range.endOffset);

  range.selectNode(mark);
  range.deleteContents();
  console.log(host.textContent, range.collapsed);
})();

Result

true
1 4 false
true false
BCD AE true
ABCDE 2 2
AE true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources