Range.toString()

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

text = range.toString();

Example

HTML

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

JavaScript

const range = document.createRange();

range.setStartBefore(document.getElementsByTagName('b').item(0), 0);
range.setEndAfter(document.getElementsByTagName('b').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