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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
---
title: selection
slug: Web/API/Selection
tags:
- API
- Experimental
- Interface
- Reference
- Selection
translation_of: Web/API/Selection
---
<div>{{ ApiRef("DOM") }}{{SeeCompatTable}}</div>
<p>A <code><strong>Selection</strong></code> object represents the range of text selected by the user or the current position of the caret. To obtain a <code>Selection</code> object for examination or manipulation, call {{DOMxRef("window.getSelection()")}}.</p>
<p>A user may make a selection from left to right (in document order) or right to left (reverse of document order). The <strong><dfn>anchor</dfn></strong> is where the user began the selection and the <strong><dfn>focus</dfn></strong> is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button, and the focus is placed where you released the mouse button.</p>
<div class="blockIndicator note">
<p><em>Anchor</em> and <em>focus</em> should not be confused with the <em>start</em> and <em>end</em> positions of a selection. The anchor can be placed before the focus or vice-versa, depending on the direction you made your selection.</p>
</div>
<h2 id="Properties">Properties</h2>
<dl>
<dt>{{DOMxRef("Selection.anchorNode")}}{{ReadOnlyInline}}</dt>
<dd>Returns the {{DOMxRef("Node")}} in which the selection begins. Can return <code>null</code> if selection never existed in the document (e.g., an iframe that was never clicked on).</dd>
<dt>{{DOMxRef("Selection.anchorOffset")}}{{ReadOnlyInline}}</dt>
<dd>Returns a number representing the offset of the selection's anchor within the <code>anchorNode</code>. If <code>anchorNode</code> is a text node, this is the number of characters within anchorNode preceding the anchor. If <code>anchorNode</code> is an element, this is the number of child nodes of the <code>anchorNode</code> preceding the anchor.</dd>
<dt>{{DOMxRef("Selection.focusNode")}}{{ReadOnlyInline}}</dt>
<dd>Returns the {{DOMxRef("Node")}} in which the selection ends. Can return <code>null</code> if selection never existed in the document (for example, in an <code>iframe</code> that was never clicked on).</dd>
<dt>{{DOMxRef("Selection.focusOffset")}}{{ReadOnlyInline}}</dt>
<dd>Returns a number representing the offset of the selection's anchor within the <code>focusNode</code>. If <code>focusNode</code> is a text node, this is the number of characters within <code>focusNode</code> preceding the focus. If <code>focusNode</code> is an element, this is the number of child nodes of the <code>focusNode</code> preceding the focus.</dd>
<dt>{{DOMxRef("Selection.isCollapsed")}}{{ReadOnlyInline}}</dt>
<dd>Returns a Boolean indicating whether the selection's start and end points are at the same position.</dd>
<dt>{{DOMxRef("Selection.rangeCount")}}{{ReadOnlyInline}}</dt>
<dd>Returns the number of ranges in the selection.</dd>
<dt>{{DOMxRef("Selection.type")}}{{ReadOnlyInline}}</dt>
<dd>Returns a {{DOMxRef("DOMString")}} describing the type of the current selection.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{DOMxRef("Selection.addRange()")}}</dt>
<dd>A {{DOMxRef("Range")}} object that will be added to the selection.</dd>
<dt>{{DOMxRef("Selection.collapse()")}}</dt>
<dd>Collapses the current selection to a single point.</dd>
<dt>{{DOMxRef("Selection.collapseToEnd()")}}</dt>
<dd>Collapses the selection to the end of the last range in the selection.</dd>
<dt>{{DOMxRef("Selection.collapseToStart()")}}</dt>
<dd>Collapses the selection to the start of the first range in the selection.</dd>
<dt>{{DOMxRef("Selection.containsNode()")}}</dt>
<dd>Indicates if a certain node is part of the selection.</dd>
<dt>{{DOMxRef("Selection.deleteFromDocument()")}}</dt>
<dd>Deletes the selection's content from the document.</dd>
<dt>{{DOMxRef("Selection.empty()")}}</dt>
<dd>Removes all ranges from the selection. This is an alias for <code>removeAllRanges()</code> — See {{DOMxRef("Selection.removeAllRanges()")}} for more details.</dd>
<dt>{{DOMxRef("Selection.extend()")}}</dt>
<dd>Moves the focus of the selection to a specified point.</dd>
<dt>{{DOMxRef("Selection.getRangeAt()")}}</dt>
<dd>Returns a {{DOMxRef("Range")}} object representing one of the ranges currently selected.</dd>
<dt>{{DOMxRef("Selection.modify()")}}{{Non-standard_Inline}}</dt>
<dd>Changes the current selection.</dd>
<dt>{{DOMxRef("Selection.removeRange()")}}</dt>
<dd>Removes a range from the selection.</dd>
<dt>{{DOMxRef("Selection.removeAllRanges()")}}</dt>
<dd>Removes all ranges from the selection.</dd>
<dt>{{DOMxRef("Selection.selectAllChildren()")}}</dt>
<dd>Adds all the children of the specified node to the selection.</dd>
<dt>{{DOMxRef("Selection.setBaseAndExtent()")}}</dt>
<dd>Sets the selection to be a range including all or parts of two specified DOM nodes, and any content located between them.</dd>
<dt>{{DOMxRef("Selection.setPosition()")}}</dt>
<dd>Collapses the current selection to a single point. This is an alias for <code>collapse()</code> — See {{DOMxRef("Selection.collapse()")}} for more details.</dd>
<dt>{{DOMxRef("Selection.toString()")}}</dt>
<dd>Returns a string currently being represented by the selection object, i.e. the currently selected text.</dd>
</dl>
<h2 id="Notes">Notes</h2>
<h3 id="String_representation_of_a_selection">String representation of a selection</h3>
<p>Calling the {{DOMxRef("Selection.toString()")}} method returns the text contained within the selection, e.g.:</p>
<pre class="brush: js notranslate">var selObj = window.getSelection();
window.alert(selObj);
</pre>
<p>Note that using a selection object as the argument to <code>window.alert</code> will call the object's <code>toString</code> method.</p>
<h3 id="Multiple_ranges_in_a_selection">Multiple ranges in a selection</h3>
<p>A selection object represents the {{DOMxRef("Range")}}s that the user has selected. Typically, it holds only one range, accessed as follows:</p>
<div style="overflow: hidden;">
<pre class="brush:js notranslate"><code>var selObj = window.getSelection();
var range = selObj.getRangeAt(0);</code></pre>
</div>
<ul>
<li><code><var>selObj</var></code> is a Selection object</li>
<li><code><var>range</var></code> is a {{DOMxRef("Range")}} object</li>
</ul>
<p>As the <a href="http://www.w3.org/TR/selection-api/#h_note_15">Selection API specification notes</a>, the Selection API was initially created by Netscape and allowed multiple ranges (for instance, to allow the user to select a column from a {{HTMLElement("table")}}). However, browsers other than Gecko did not implement multiple ranges, and the specification also requires the selection to always have a single range.</p>
<h3 id="Selection_and_input_focus">Selection and input focus</h3>
<p>Selection and input focus (indicated by {{DOMxRef("Document.activeElement")}}) have a complex relationship that varies by browser. In cross-browser compatible code, it's better to handle them separately.</p>
<p>Safari and Chrome (unlike Firefox) currently focus the element containing selection when modifying the selection programmatically; it's possible that this may change in the future (see <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=14383">W3C bug 14383</a> and {{WebKitBug("38696")}}).</p>
<h3 id="Behavior_of_Selection_API_in_terms_of_editing_host_focus_changes">Behavior of Selection API in terms of editing host focus changes</h3>
<p>The Selection API has a common behavior (i.e., shared between browsers) that governs how focus behavior changes for <dfn>editing hosts</dfn> after certain methods are called.</p>
<p>The behavior is as follows:</p>
<ol>
<li>An editing host gains focus if the previous selection was outside of it.</li>
<li>A Selection API method is called, causing a new selection to be made with the selection range inside the editing host.</li>
<li>Focus then moves to the editing host.</li>
</ol>
<div class="note">
<p><strong>Note</strong>: The Selection API methods may only move focus to an editing host, not to other focusable elements (e.g., {{HTMLElement("a")}}).</p>
</div>
<p>The above behavior applies to selections made using the following methods:</p>
<ul>
<li>{{DOMxRef("Selection.collapse()")}}</li>
<li>{{DOMxRef("Selection.collapseToStart()")}}</li>
<li>{{DOMxRef("Selection.collapseToEnd()")}}</li>
<li>{{DOMxRef("Selection.extend()")}}</li>
<li>{{DOMxRef("Selection.selectAllChildren()")}}</li>
<li>{{DOMxRef("Selection.addRange()")}}</li>
<li>{{DOMxRef("Selection.setBaseAndExtent()")}}</li>
</ul>
<p>And when the {{DOMxRef("Range")}} is modified using the following methods:</p>
<ul>
<li>{{DOMxRef("Range.setStart()")}}</li>
<li>{{DOMxRef("Range.setEnd()")}}</li>
<li>{{DOMxRef("Range.setStartBefore()")}}</li>
<li>{{DOMxRef("Range.setStartAfter()")}}</li>
<li>{{DOMxRef("Range.setEndBefore()")}}</li>
<li>{{DOMxRef("Range.setEndAfter()")}}</li>
<li>{{DOMxRef("Range.collapse()")}}</li>
<li>{{DOMxRef("Range.selectNode()")}}</li>
<li>{{DOMxRef("Range.selectNodeContents()")}}</li>
</ul>
<h3 id="Glossary">Glossary</h3>
<p>Other key terms used in this section.</p>
<dl>
<dt>anchor</dt>
<dd>The anchor of a selection is the beginning point of the selection. When making a selection with a mouse, the anchor is where in the document the mouse button is initially pressed. As the user changes the selection using the mouse or the keyboard, the anchor does not move.</dd>
<dt>editing host</dt>
<dd>An editable element (e.g., an HTML element with {{htmlattrxref("contenteditable")}} set, or the HTML child of a document that has {{DOMxRef("Document.designMode", "designMode")}} enabled).</dd>
<dt>focus of a selection</dt>
<dd>The <dfn>focus</dfn> of a selection is the end point of the selection. When making a selection with a mouse, the focus is where in the document the mouse button is released. As the user changes the selection using the mouse or the keyboard, the focus is the end of the selection that moves.
<div class="note">This is not the same as the focused <em>element</em> of the document, as returned by {{DOMxRef("document.activeElement")}}.</div>
</dd>
<dt>range</dt>
<dd>A <dfn>range</dfn> is a contiguous part of a document. A range can contain entire nodes as well as portions of nodes (such as a portion of a text node). A user will normally only select a single range at a time, but it's possible for a user to select multiple ranges (e.g., by using the <kbd>Control</kbd> key). A range can be retrieved from a selection as a {{DOMxRef("range")}} object. Range objects can also be created via the DOM and programmatically added or removed from a selection.</dd>
</dl>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName("Selection API", "#selection-interface", "Selection")}}</td>
<td>{{Spec2("Selection API")}}</td>
<td>The Selection API specification is based on the HTML Editing APIs specification and focuses on the Selection-related functionality.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.Selection")}}</p>
<h3 id="Gecko_notes">Gecko notes</h3>
<ul>
<li>Gecko/Firefox provide additional features, available to chrome (internal and add-on) code only. These are defined in {{Interface("nsISelectionPrivate")}}.</li>
<li>Mozilla source code: {{Source("dom/webidl/Selection.webidl")}}</li>
<li>{{DOMxRef("Selection.selectionLanguageChange()")}}{{Obsolete_Inline("gecko29")}} used to be exposed to the web content until Firefox 29</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
<li>{{DOMxRef("Window.getSelection")}}, {{DOMxRef("Document.getSelection")}}, {{DOMxRef("Range")}}</li>
<li>Selection-related events: {{Event("selectionchange")}} and {{Event("selectstart")}}</li>
<li>HTML inputs provide simpler helper APIs for working with selection (see {{DOMxRef("HTMLInputElement.setSelectionRange()")}})</li>
<li>{{DOMxRef("Document.activeElement")}}, {{DOMxRef("HTMLElement.focus()")}}, and {{DOMxRef("HTMLElement.blur()")}}</li>
</ul>
|