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.


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


Required. String that specifies the type of unit to expand by.
One of the following values:
Expands by a character at the end of the current TextRange.
Moves the start and end points of the current TextRange object to the start and end positions of the next sentence.
Aligns the current TextRange to the contents of the entire document.
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:
    <script type="text/javascript">
        function SelectWholeWords () {
            if (document.selection) {        // Internet Explorer
                var myRange = document.selection.createRange ();
                myRange.expand ("word");                                        
    Some text for selection.
    <br /><br />
    <button onclick="SelectWholeWords ()">Select whole words!</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content