HTMLInputElement.setSelectionRange()

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.

O método**HTMLInputElement.setSelectionRange()** define as posições inicial e final da seleção atual do texto em um elemento .

Opcionalmente, em navegadores mais novos, você pode especificar a direção na qual a seleção deve ser feita; isso permite a você indicar, por exemplo, que a seleção foi feita como se o usuário tivesse clicado no fim do texto selecionado e arrastado em direção ao início.

Esse método atualiza ao mesmo tempo HTMLInputElement.selectionStart, selectionEnd, and selectionDirection.

Syntax

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

Parameters

selectionStart

The 0-based index of the first selected character.

selectionEnd

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.

Example

The following code:

html


  
    
    JS Bin
    
  
  
    

will produce the following:

example.png

Especificações

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

Compatibilidade com navegadores

See also