The HTMLInputElement.setSelectionRange() method sets the start and end positions of the current text selection in an <input> element.

Optionally, in newer browser versions, you can specify the direction in which selection should be considered to have occurred; this lets you indicate, for example, that the selection as set by the user clicking and dragging from the end of the selected text toward the beginning.

This method updates HTMLInputElement.selectionStart, selectionEnd, and selectionDirection in one call.


inputElement.setSelectionRange(selectionStart, selectionEnd[, selectionDirection]);


The 0-based index of the first selected character.
The 0-based index of the character after the last selected character.
selectionDirection Optional
A string indicating the direction in which the selection is performed. This string can be "forward" or "backward", or "none" if the direction is unknown or irrelevant.


The following code:

<!DOCTYPE html>
<meta charset=utf-8>
<title>JS Bin</title>
function SelectText () {
        var input = document.getElementById("mytextbox");
  <p><input type="text" id="mytextbox" size="20" value="Mozilla"/></p>
  <p><button onclick="SelectText()">Select text</button></p>

will produce the following:



Specification Status Comment
HTML Living Standard
The definition of 'HTMLInputElement.setSelectionRange()' in that specification.
Living Standard No change
HTML 5.1
The definition of 'HTMLInputElement.setSelectionRange()' in that specification.
Recommendation No change
The definition of 'HTMLInputElement.setSelectionRange()' in that specification.
Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support 9Opera Full support 8Safari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?


Full support  
Full support
Compatibility unknown  
Compatibility unknown

See also