Window.getSelection

  • Revision slug: Web/API/window.getSelection
  • Revision title: window.getSelection
  • Revision id: 408449
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef() }}

Summary

Returns a selection object representing the range of text selected by the user.

Syntax

selection = window.getSelection() ;

Example

function foo() {
    var selObj = window.getSelection(); 
    alert(selObj);
    var selRange = selObj.getRangeAt(0);
    // do stuff with the range
}

Notes

In JavaScript, when an object is passed to a function expecting a string (like {{ Domxref("window.alert") }} or {{ Domxref("document.write") }}), the object's 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.

In the above example, selObj.toString() is automatically called when it is passed to window.alert. However, attempting to use a JavaScript String {{todo("deadLink")}} property or method such as length {{todo("deadLink")}} or substr {{todo("deadLink")}} directly on a Selection object will result in an error if it does not have that property or method and may return unexpected results if it does. To use a Selection object as a string, call its toString method directly:

var selectedText = selObj.toString();
  • selObj is a Selection object
  • selectedText is a string

Specification

{{dom0()}}

It is expected to be specified in a new DOM Range spec (taken out of HTML5).

See also DOM HTML5 Range, Extensions to other interfaces

See also

Revision Source

<div>
  {{ DomRef() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a selection object representing the range of text selected by the user. <!-- NB. this is nothing to do with the DOM selection list object! What the heck is 'selection list object'? -Nickolay --></p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<em>selection</em> = <em>window</em>.getSelection() ;</pre>
<ul>
  <li><code>selection</code> is a <a href="/en-US/docs/DOM/Selection" title="DOM/Selection">Selection</a> object.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
function foo() {
    var selObj = window.getSelection(); 
    alert(selObj);
    var selRange = selObj.getRangeAt(0);
    // do stuff with the range
}</pre>
<h2 id="Notes" name="Notes">Notes</h2>


<p>In JavaScript, when an object is passed to a function expecting a string (like {{ Domxref("window.alert") }} or {{ Domxref("document.write") }}), the object's <code>toString()</code> 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 <code><a href="/en-US/docs/DOM/window.alert" title="DOM/window.alert">window.alert</a></code>. However, attempting to use a JavaScript <a href="/en-US/docs/JS/String" title="JS/String">String</a> {{todo("deadLink")}} property or method such as <code><a href="/en-US/docs/JS/String.prototype.length" title="JS/String.prototype.length">length</a></code> {{todo("deadLink")}} or <code><a href="/en-US/docs/JS/String.prototype.substr" title="JS/String.prototype.substr">substr</a></code> {{todo("deadLink")}} directly on a Selection object will result in an error if it does not have that property or method and may return unexpected results if it does. To use a Selection 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 Selection object</li>
  <li><code>selectedText</code> is a string</li>
</ul>
<h2 id="Specification" name="Specification">Specification</h2>
<p>{{dom0()}}</p>
<p>It is expected to be specified in a new <a class="external" href="http://html5.org/specs/dom-range.html">DOM Range spec</a> (<a class="external" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=10798" title="http://www.w3.org/Bugs/Public/show_bug.cgi?id=10798">taken out of HTML5</a>).</p>
<p>See also DOM HTML5 Range, <a class="external" href="http://html5.org/specs/dom-range.html#extensions-to-other-interfaces">Extensions to other interfaces</a></p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/DOM/Selection" title="DOM/Selection">Selection</a></li>
  <li><a href="/en-US/docs/DOM/Range" title="DOM/Range">Range</a></li>
</ul>
<!--languages({
"es": "es/DOM/window.getSelection",
"fr": "fr/DOM/window.getSelection",
"it": "it/DOM/window.getSelection",
"ja": "ja/DOM/window.getSelection",
"pl": "pl/DOM/window.getSelection"
}) -->
Revert to this revision