Open In App

HTML DOM Range toString() Method

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Range toString() method is used to convert range to String text. The selected Range is an object which need to be converted to text , so this method is used.

Syntax:

Range.toString();

Parameters: This method does not accept any parameters.

Return value: This method return the string text of the specified range.

Example: This example shows how to convert a range to string text. We used setStartBefore() method and setEndAfter() method to set the range for our example.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM range toString() property
    </title>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
 
     
<p>
        The range starts from italics
        text <b><i>RangeStart</i></b>
        "This is the Range text" <b>
        <i>RangeEnd</i></b>
    </p>
 
 
    Range in String Text is below:
    <b><p id="text"></p>
</b>
 
    <script>
        const range = document.createRange();
        range.setStartBefore(document
            .getElementsByTagName('i').item(0));
 
        range.setEndAfter(document
            .getElementsByTagName('i').item(1));
 
        document.getElementById('text')
            .innerHTML = range.toString();
             
        console.log(range);
        console.log(range.toString());
    </script>
</body>
 
</html>


Output: The innerHTML of text element contains the range in text format.

For Reference, the range object and text range are logged in console for better clarification about this method.

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Internet Explorer 9
  • Apple Safari 1
  • Opera 9
     


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads