setSelectionRange method
Sets the start and end positions of a selection in a text field.
![]() |
Syntax
object.setSelectionRange(start, end, direction)Parameters
- start [in]
-
Type: Integer
The offset into the text field for the start of the selection.
- end [in]
-
Type: Integer
The offset into the text field for the end of the selection.
- direction [in, optional]
-
Type: DOMString
The direction of the text selection.
Return value
Type: HRESULT
If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
Standards information
- HTML 5.1, Section 4.10.20
Remarks
If you set a parameter to more than the length of the text field, the parameter points to the end of the text field. If the end parameter is less than or equal to the start paramenter, the start and end positions of the selection are set to the end value. The selection is then an insertion point or caret.
The direction parameter of the selection must be set to "backward" if direction is a case-sensitive match for the string "backward", "forward" if direction is a case-sensitive match for the string "forward", or "none" if the platform does not support selections or otherwise (including if the argument is omitted)
Examples
The following code example shows how to set a test selection's start and end positions.
<!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=9" /> <!--Force IE9 mode --> <title>Example of setSelectionRange()</title> <script type="text/javascript"> function SelectSomeText () { var input = document.getElementById ("Textbox"); input.setSelectionRange (4,13); } </script> </head> <body> <p><input type="text" id="Textbox" size="40" value="The text selection appears here"/></p> <p><button onclick="SelectSomeText ()">See selection</button></p> </body> </html>
See also
- input type=email
- input type=number
- input type=range
- input type=search
- input type=tel
- input type=text
- input type=url
- textArea
