This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The setSelectionRange() method of the HTMLTextAreaElement interface sets the start and end positions of the current text selection, and optionally the direction, in a <textarea> element. This updates the selection state immediately, though the visual highlight only appears when the element is focused. The direction indicates the in which selection should be considered to have occurred; for example, that the selection was set by the user clicking and dragging from the end of the selected text toward the beginning. In addition, the select and selectionchange events are fired.
This method updates the HTMLTextAreaElement.selectionStart, HTMLTextAreaElement.selectionEnd, and HTMLTextAreaElement.selectionDirection properties immediately, regardless of focus state. The visual selection highlight requires the element to be focused.
Note: While setSelectionRange() updates the selection properties immediately, the visual selection highlight only appears when the <textarea> is focused. Focusing the element will also fire a selectionchange event.
To select all of the text of an <textarea> element, use the HTMLTextAreaElement.select() method.
setSelectionRange(selectionStart, selectionEnd) setSelectionRange(selectionStart, selectionEnd, selectionDirection)
selectionStartThe 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. See the selectionStart property for more information.
selectionEndThe 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. See the selectionEnd property for more information.
selectionDirection OptionalThe keyword "forward", "backward", or the default "none" — indicating the direction in which the selection is considered to have been performed. See the selectionDirection property for more information.
None (undefined).
const textarea = document.getElementById("text-box");
const chars = textarea.textLength;
// if the value is more than 10 characters long
if (chars > 10) {
// Element must be focused to select a range of text within it
textarea.focus();
// select the text between the fifth character from the start and
// the fifth character from the end
textarea.setSelectionRange(5, chars - 5);
} else {
// otherwise select all the text
textarea.select();
}
| Specification |
|---|
| HTML> # dom-textarea/input-setselectionrange-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
setSelectionRange |
1 | 12 | 1 | ≤12.1 | 1.3 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
<textarea>HTMLTextAreaElementHTMLTextAreaElement.select()HTMLTextAreaElement.textLengthSelection::selection pseudo-element
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setSelectionRange