HTML DOM Range toString() Method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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
  • Edge
  • Firefox
  • Apple Safari
  • Opera
     

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.