You are here: Reference > JavaScript > client-side > selection and ranges > methods > expand (TextRange)

expand method (TextRange)

Browser support:
Expands the contents of the TextRange from the insertion point by a character, sentence or word.
If you need to move only the start or end point of a TextRange, use the moveStart and moveEnd methods.

Syntax:

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

Parameters:

unit
Required. String that specifies the type of unit to expand by.
One of the following values:
character
Expands by a character at the end of the current TextRange.
sentence
Moves the start and end points of the current TextRange object to the start and end positions of the next sentence.
textedit
Aligns the current TextRange to the contents of the entire document.
word
Expands to a word at both ends of the current TextRange object. Word characters mean alphanumeric characters.

Return value:

Boolean. One of the following values:
false The range was not expanded.
true The range was successfully expanded.

Example HTML code 1:

This example illustrates the use of the expand method:
<head>
    <script type="text/javascript">
        function SelectWholeWords () {
            if (document.selection) {        // Internet Explorer
                var myRange = document.selection.createRange ();
                myRange.expand ("word");                                        
                myRange.select ();
            }
        }
    </script>
</head>
<body>
    Some text for selection.
    <br /><br />
    <button onclick="SelectWholeWords ()">Select whole words!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content