HTMLTextAreaElement: setSelectionRange() method

Baseline
Widely available

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 in a <textarea> element.

For general information about this method, see HTMLInputElement.setSelectionRange().

Syntax

js
setSelectionRange(selectionStart, selectionEnd)
setSelectionRange(selectionStart, selectionEnd, selectionDirection)

Parameters

selectionStart

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. See the selectionStart property.

selectionEnd

The 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. See the selectionEnd property.

selectionDirection Optional

A string indicating the direction in which the selection is considered to have been performed. See the selectionDirection property. Possible values:

  • "forward"
  • "backward"
  • "none" if the direction is unknown or irrelevant. Default value.

Return value

None (undefined).

Specifications

Specification
HTML
# dom-textarea/input-setselectionrange-dev

Browser compatibility

See also