You are here: Reference > JavaScript > client-side > selection and ranges > methods > toString (Range, selectionRange)

toString method (Range, selectionRange)

Browser support:
9
Returns the text content of a Range or selectionRange object.
Note: The selectionRange object and its toString method are supported in Internet Explorer from version 9.
In older Internet Explorer versions (and optionally in newer ones as well), use the text property of the TextRange object for similar functionality.

Syntax:

object.toString ( );
You can find the related objects in the Supported by objects section below.

Return value:

String that retrieves the text content.

Example HTML code 1:

This example illustrates the use of the toString method for selections:
<head>
    <script type="text/javascript">
        function GetSelectedText () {
            if (window.getSelection) {  // all browsers, except IE before version 9
                var selRange = window.getSelection ();
                alert (selRange.toString ());
            }
            else {
                if (document.selection) {        // Internet Explorer
                    var textRange = document.selection.createRange ();
                    alert (textRange.text);
                }
            }
        }
    </script>
</head>
<body>
    <div>Please select <b>all</b> or a <i>part</i> of this text.</div>
    <br />
    <button onclick="GetSelectedText ();">Get selected text!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content