Range: toString() method

The Range.toString() method is a stringifier returning the text of the Range.

Alerting the contents of a Range makes an implicit toString() call, so comparing range and text through an alert dialog is ineffective.

Syntax

js
toString()

Parameters

None.

Return value

A string.

Examples

HTML

html
<p>
  This example logs <em>everything</em> between the emphasized <em>words</em>.
  Look at the output below.
</p>
<p id="log"></p>

JavaScript

js
const range = document.createRange();

range.setStartBefore(document.getElementsByTagName("em").item(0), 0);
range.setEndAfter(document.getElementsByTagName("em").item(1), 0);
document.getElementById("log").textContent = range.toString();

Result

Specifications

Specification
DOM Standard
# dom-range-stringifier

Browser compatibility

BCD tables only load in the browser

See also