Skip to content
Related Articles

Related Articles

HTML DOM Range toString() Method
  • Last Updated : 08 Jul, 2020

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
  • Edge
  • Firefox
  • Apple Safari
  • Opera
     
My Personal Notes arrow_drop_up
Recommended Articles
Page :