blob: f3eca58f61a3e1d75968be6e232e133ae83967ce (
plain)
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
|
---
title: DocumentOrShadowRoot.getSelection()
slug: Web/API/Document/getSelection
translation_of: Web/API/DocumentOrShadowRoot/getSelection
original_slug: Web/API/DocumentOrShadowRoot/getSelection
---
<div>{{APIRef("DOM")}}{{SeeCompatTable}}</div>
<p><span class="seoSummary">The <strong><code>getSelection()</code></strong> property of the {{DOMxRef("DocumentOrShadowRoot")}} interface returns a {{DOMxRef("Selection")}} object representing the range of text selected by the user, or the current position of the caret.</span></p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">var selection = documentOrShadowRootInstance.getSelection()</pre>
<h3 id="Parameters">Parameters</h3>
<p>None.</p>
<h3 id="Returns">Returns</h3>
<p>A {{DOMxRef("Selection")}} object.</p>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">function foo() {
var selObj = document.getSelection();
alert(selObj);
var selRange = selObj.getRangeAt(0);
// do stuff with the range
}</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<h3 id="String_representation_of_the_Selection_object">String representation of the Selection object</h3>
<p>In JavaScript, when an object is passed to a function expecting a string (like {{DOMxRef("Window.alert()")}}), the object's {{JSxRef("Object.toString", "toString()")}} method is called and the returned value is passed to the function. This can make the object appear to be a string when used with other functions when it is really an object with properties and methods.</p>
<p>In the above example, <code>selObj.toString()</code> is automatically called when it is passed to {{DOMxRef("Window.alert()")}}. However, attempting to use a JavaScript <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" title="JS/String">String</a> property or method such as <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length" title="JS/String.length">length</a></code> or <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr" title="JS/String.substr">substr</a></code> directly on a {{DOMxRef("Selection")}} object results in an error if it does not have that property or method and may return unexpected results if it does. To use a <code>Selection</code> object as a string, call its <code>toString()</code> method directly:</p>
<pre class="brush:js;gutter:false;">var selectedText = selObj.toString();</pre>
<ul>
<li><code>selObj</code> is a <code>Selection</code> object.</li>
<li><code>selectedText</code> is a string (Selected text).</li>
</ul>
<h3 id="Related_objects">Related objects</h3>
<p>HTML inputs provide simpler helper APIs for working with selection (see {{DOMxRef("HTMLInputElement.setSelectionRange()")}}).</p>
<p>Notice the difference between <em>selection</em> and <em>focus</em>. {{DOMxRef("Document.activeElement")}} returns the focused element.</p>
<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("Shadow DOM", "#extensions-to-the-documentorshadowroot-mixin", "DocumentOrShadowRoot")}}</td>
<td>{{Spec2("Shadow DOM")}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_Compatibility">Browser Compatibility</h2>
<div>
<p>{{Compat("api.DocumentOrShadowRoot.getSelection")}}</p>
</div>
|