Baseline Widely available
O método**HTMLInputElement.setSelectionRange()
** define as posições inicial e final da seleção atual do texto em um elemento <input>
.
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
.
inputElement.setSelectionRange(selectionStart, selectionEnd, [optional] selectionDirection);Parameters
The 0-based index of the first selected character.
The 0-based index of the character after the last selected character.
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>
<html>
<head>
<meta charset="utf-8" />
<title>JS Bin</title>
<script>
function SelectText() {
var input = document.getElementById("mytextbox");
input.focus();
input.setSelectionRange(2, 5);
}
</script>
</head>
<body>
<p><input type="text" id="mytextbox" size="20" value="Mozilla" /></p>
<p><button onclick="SelectText()">Select text</button></p>
</body>
</html>
will produce the following:
Especificações Compatibilidade com navegadores See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4