--- title: HTMLInputElement.setSelectionRange() slug: Web/API/HTMLInputElement/setSelectionRange translation_of: Web/API/HTMLInputElement/setSelectionRange ---
O métodoHTMLInputElement.setSelectionRange()
define as posições inicial e final da seleção atual do texto em um elemento {{HTMLElement("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);
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:
Specification | Status | Comment |
---|---|---|
{{SpecName("HTML WHATWG", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}} | {{Spec2("HTML WHATWG")}} | No change |
{{SpecName("HTML5.1", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}} | {{Spec2("HTML5.1")}} | No change |
{{SpecName("HTML5 W3C", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}} | {{Spec2("HTML5 W3C")}} | Initial definition |
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1.0 | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("1.0")}} | 9 | 8.0 | {{CompatVersionUnknown}} |
selectionDirection |
15[1] | {{CompatVersionUnknown}} | {{CompatGeckoDesktop("8.0")}}[2] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatVersionUnknown}}[3] |
Feature | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | Yes | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | No |
selectionDirection |
{{CompatVersionUnknown}} | {{CompatGeckoMobile("8.0")}}[2] | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
[1] The support for selectionDirection
was added Blink in {{WebkitBug("60403")}}.
Note that accordingly to the WHATWG forms spec selectionStart
, selectionEnd
properties and setSelectionRange
method apply only to inputs of types text, search, URL, tel and password. Chrome, starting from version 33, throws an exception while accessing those properties and method on the rest of input types. For example, on input of type number: "Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('number') does not support selection." Related links: question on StackOverflow, whatwg bug, Chromium bug.
[2] The support for selectionDirection
was added to Gecko in {{bug("674558")}}.
[3] The support for selectionDirection
was added Webkit in {{WebKitBug("60403")}}.