W3cubDocs

/Web APIs

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

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 4.4 18 4 10.1 1 1.0

See also

© 2005–2023 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