Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The HTMLInputElement.setSelectionRange() method sets the start and end positions of the current text selection in an <input> or <textarea> element. This updates the selection state immediately, though the visual highlight only appears when the element is focused.
Optionally, you can specify the direction in which selection should be considered to have occurred. This lets you indicate, for example, that the selection was set by the user clicking and dragging from the end of the selected text toward the beginning.
This method updates the HTMLInputElement.selectionStart, HTMLInputElement.selectionEnd, and HTMLInputElement.selectionDirection properties in one call, regardless of whether the element is focused. The visual selection highlight will only appear when the element has focus.
The element must be of one of the following input types: password, search, tel, text, or url. Otherwise the browser throws an InvalidStateError exception.
If you wish to select all text of an input element, you can use the HTMLInputElement.select() method instead.
The 0-based index of the first selected character. An index greater than the length of the element's value is treated as pointing to the end of the value.
selectionEndThe 0-based index of the character after the last selected character. An index greater than the length of the element's value is treated as pointing to the end of the value. If selectionEnd is less than selectionStart, then both are treated as the value of selectionEnd.
selectionDirection OptionalA string indicating the direction in which the selection is considered to have been performed. Possible values:
None (undefined).
Thrown if the element is not one of the following input types: password, search, tel, text, or url.
Click the button in this example to select the third, fourth, and fifth characters in the text box ("zil" in the word "Mozilla").
| HTML # dom-textarea/input-setselectionrange-dev |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 30, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.