Table of Contents

What is a Javascript DOM Range? A sequence of characters

About

A range represent a sequence of content at the character level within a node tree.

They are used in editing for

Example

Below we will define the range that represents the olor sit ame

<p>Lorem ipsum dolor sit <em>amet, consectetur</em> adipiscing elit</p>
let p = document.querySelector("p");
let em = document.querySelector("em");
let range = new Range(),
      pTextNode = p.childNodes[0], 
      emTextNode = em.firstChild;
range.setStart(pTextNode, 13); // from the 13 characters of the p element
range.setEnd(emTextNode, 3); // until the third character of em
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

Concept

A range has two associated boundary points

Boundary point

A boundary point consists:

Collpased

A collapsed Range is empty and specifies only a single-point in the DOM tree.