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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
toString
1
12
1
9
9
1
1
18
4
10.1
1
1.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Range/toString