1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
---
title: HTMLInputElement.setSelectionRange()
slug: Web/API/HTMLInputElement/setSelectionRange
translation_of: Web/API/HTMLInputElement/setSelectionRange
---
<div>{{APIRef("HTML DOM")}}</div>
<p>O método<strong><code>HTMLInputElement.setSelectionRange()</code></strong> define as posições inicial e final da seleção atual do texto em um elemento {{HTMLElement("input")}}.</p>
<p>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.</p>
<p>Esse método atualiza ao mesmo tempo <code>HTMLInputElement.selectionStart</code>, <code>selectionEnd</code>, and <code>selectionDirection</code>.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">inputElement.setSelectionRange(<var>selectionStart</var>, <var>selectionEnd</var>, [optional] <var>selectionDirection</var>);
</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt><em>selectionStart</em></dt>
<dd>The 0-based index of the first selected character.</dd>
<dt><em>selectionEnd</em></dt>
<dd>The 0-based index of the character <em>after</em> the last selected character.</dd>
<dt><em>selectionDirection</em> {{optional_inline}}</dt>
<dd>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.</dd>
</dl>
<h2 id="Example">Example</h2>
<p>The following code:</p>
<pre class="brush: html"><!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>
</pre>
<p>will produce the following:</p>
<p><img alt="example.png" class="default internal" src="/@api/deki/files/6104/=example.png"></p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("HTML WHATWG", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}}</td>
<td>{{Spec2("HTML WHATWG")}}</td>
<td>No change</td>
</tr>
<tr>
<td>{{SpecName("HTML5.1", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}}</td>
<td>{{Spec2("HTML5.1")}}</td>
<td>No change</td>
</tr>
<tr>
<td>{{SpecName("HTML5 W3C", "forms.html#dom-textarea/input-setselectionrange", "HTMLInputElement.setSelectionRange()")}}</td>
<td>{{Spec2("HTML5 W3C")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>1.0</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("1.0")}}</td>
<td>9</td>
<td>8.0</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
<tr>
<td><code>selectionDirection</code></td>
<td>15<sup>[1]</sup></td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("8.0")}}<sup>[2]</sup></td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}<sup>[3]</sup></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table" style="height: 104px; width: 1023px;">
<tbody>
<tr>
<th>Feature</th>
<th>Edge</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>Yes</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>No</td>
</tr>
<tr>
<td><code>selectionDirection</code></td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("8.0")}}<sup>[2]</sup></td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td> </td>
</tr>
</tbody>
</table>
</div>
<p>[1] The support for <code>selectionDirection</code> was added Blink in {{WebkitBug("60403")}}.</p>
<p>Note that accordingly to the <a href="https://html.spec.whatwg.org/multipage/forms.html#concept-input-apply">WHATWG forms spec</a> <code>selectionStart</code>, <code>selectionEnd</code> properties and <code>setSelectionRange</code> 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: <a href="http://stackoverflow.com/questions/21177489/selectionstart-selectionend-on-input-type-number-no-longer-allowed-in-chrome">question on StackOverflow</a>, <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=24796">whatwg bug</a>, <a href="https://code.google.com/p/chromium/issues/detail?id=324360">Chromium bug</a>.</p>
<p>[2] The support for <code>selectionDirection</code> was added to Gecko in {{bug("674558")}}.</p>
<p>[3] The support for <code>selectionDirection</code> was added Webkit in {{WebKitBug("60403")}}.</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{HTMLElement("input")}}</li>
<li>{{domxref("HTMLInputElement")}}</li>
<li>{{domxref("Selection")}}</li>
</ul>
|