Window.getSelection

  • Revision slug: DOM/window.getSelection
  • Revision title: window.getSelection
  • Revision id: 239005
  • Created:
  • Creator: m_gol
  • Is current revision? No
  • Comment range -> Range; one or more formatting changes

Revision Content

{{ DomRef() }}

Summary

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

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 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 property or method such as length or substr 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

DOM Level 0. Not part of specification.

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

Selection, Range

 

{{ 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" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Returns a selection object representing the range of text selected by the user. <span class="comment">NB. this is nothing to do with the DOM selection list object! What the heck is 'selection list object'? -Nickolay</span></p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval"><em>selection</em> = <em>window</em>.getSelection() ;
</pre>
<ul> <li><code>selection</code> is a <a href="/en/DOM/Selection" title="en/DOM/Selection">Selection</a> object.</li>
</ul>
<h3 name="Example">Example</h3>
<pre class="brush: js">function foo() {
   var selObj = window.getSelection(); 
   alert(selObj);
   var selRange = selObj.getRangeAt(0);
   // do stuff with the range
}
</pre>
<h3 name="Notes">Notes</h3>
<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 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/DOM/window.alert" title="en/DOM/window.alert">window.alert</a></code>. However, attempting to use a JavaScript <a href="/en/JS/String" title="en/JS/String">String</a> property or method such as <code><a href="/en/JS/String.prototype.length" title="en/JS/String.prototype.length">length</a></code> or <code><a href="/en/JS/String.prototype.substr" title="en/JS/String.prototype.substr">substr</a></code> 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">  var selectedText = selObj.toString();
</pre>
<ul> <li><code>selObj</code> is a Selection object</li> <li><code>selectedText</code> is a string</li>
</ul>
<h3 name="Specification">Specification</h3>
<p>DOM Level 0. Not part of specification.</p>
<p>It is expected to be specified in a new <a class="external" href="http://html5.org/specs/dom-range.html" title="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" title="http://html5.org/specs/dom-range.html#extensions-to-other-interfaces">Extensions to other interfaces</a></p>
<h3 name="See_also">See also</h3>
<p><a href="/en/DOM/Selection" title="en/DOM/Selection">Selection</a>, <a href="/en/DOM/Range" title="en/DOM/Range">Range</a></p>
<p> </p>
<p>{{ 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" } ) }}</p>
Revert to this revision