Selection

  • Revision slug: DOM/Selection
  • Revision title: Selection
  • Revision id: 303381
  • Created:
  • Creator: JeanDavidDaviet
  • Is current revision? No
  • Comment Because it lacked the method "empty". And others method I didn't write.

Revision Content

{{DomRef()}}

Introduction

Selection is the class of the object returned by {{domxref("window.getSelection()")}} and other methods. It represents the text selection in the greater page, possibly spanning multiple elements, when the user drags over static text and other parts of the page. For information about text selection in an individual text editing element, see {{domxref("HTMLInputElement","Input")}}, {{domxref("HTMLTextAreaElement","TextArea")}} and {{domxref("document.activeElement")}} which typically return the parent object returned from {{domxref("window.getSelection()")}}.

A selection object represents the {{domxref("range","ranges")}} that the user has selected. Typically, it holds only one range, accessed as follows:

var selObj = window.getSelection();
var range  = selObj.getRangeAt(0);
  • selObj is a Selection object
  • range is a Range object

Calling the {{domxref("Selection/toString()","")}} method returns the text contained in the selection, e.g

var selObj = window.getSelection();
window.alert(selObj); 

Note that using a selection object as the argument to window.alert will call the object's toString method.

Glossary

Other key terms used in this section.

anchor
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.
focus
The focus 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.
range
A range 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 Control 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.

Properties

{{domxref("Selection/anchorNode","anchorNode")}}
Returns the node in which the selection begins.
{{domxref("Selection/anchorOffset","anchorOffset")}}
Returns the number of characters that the selection's anchor is offset within the anchorNode.
{{domxref("Selection/focusNode","focusNode")}}
Returns the node in which the selection ends.
{{domxref("Selection/focusOffset","focusOffset")}}
Returns the number of characters that the selection's focus is offset within the focusNode.
{{domxref("Selection/isCollapsed","isCollapsed")}}
Returns a Boolean indicating whether the selection's start and end points are at the same position.
{{domxref("Selection/rangeCount","rangeCount")}}
Returns the number of ranges in the selection.

Methods

{{domxref("Selection/getRangeAt","getRangeAt")}}
Returns a range object representing one of the ranges currently selected.
{{domxref("Selection/collapse","collapse")}}
Collapses the current selection to a single point.
{{domxref("Selection/empty","empty")}}
Empty the current selection.
{{domxref("Selection/extend","extend")}}
Moves the focus of the selection to a specified point.
{{domxref("Selection/modify","modify")}} {{gecko_minversion_inline("2.0")}}
Changes the current selection.
{{domxref("Selection/collapseToStart","collapseToStart")}}
Collapses the selection to the start of the first range in the selection.
{{domxref("Selection/collapseToEnd","collapseToEnd")}}
Collapses the selection to the end of the last range in the selection.
{{domxref("Selection/selectAllChildren","selectAllChildren")}}
Adds all the children of the specified node to the selection.
{{domxref("Selection/addRange","addRange")}}
A range object that will be added to the selection.
{{domxref("Selection/removeRange","removeRange")}}
Removes a range from the selection.
{{domxref("Selection/removeAllRanges","removeAllRanges")}}
Removes all ranges from the selection.
{{domxref("Selection/deleteFromDocument","deleteFromDocument")}}
Deletes the selection's content from the document.
{{domxref("Selection/selectionLanguageChange","selectionLanguageChange")}}
Modifies the cursor Bidi level after a change in keyboard direction.
{{domxref("Selection/toString","toString")}}
Returns a string currently being represented by the selection object, i.e. the currently selected text.
{{domxref("Selection/containsNode","containsNode")}}
Indicates if a certain node is part of the selection.

See also

Revision Source

<div>
  {{DomRef()}}</div>
<h2 id="Introduction" name="Introduction">Introduction</h2>
<p>Selection is the class of the object returned by {{domxref("window.getSelection()")}} and other methods. It represents the text selection in the greater page, possibly spanning multiple elements, when the user drags over static text and other parts of the page. For information about text selection in an individual text editing element, see {{domxref("HTMLInputElement","Input")}}, {{domxref("HTMLTextAreaElement","TextArea")}} and {{domxref("document.activeElement")}} which typically return the parent object returned from {{domxref("window.getSelection()")}}.</p>
<p>A selection object represents the {{domxref("range","ranges")}} that the user has selected. Typically, it holds only one range, accessed as follows:</p>
<div style="overflow:hidden">
  <pre class="brush:js">
var selObj = window.getSelection();
var range  = selObj.getRangeAt(0);</pre>
</div>
<ul>
  <li><code>selObj</code> is a Selection object</li>
  <li><code>range</code> is a <a href="/en-US/docs/DOM/range" title="DOM/Range">Range</a> object</li>
</ul>
<p>Calling the {{domxref("Selection/toString()","")}} method returns the text contained in the selection, e.g</p>
<pre class="brush: js">
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>
<h2 id="Glossary" name="Glossary">Glossary</h2>
<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>
    focus</dt>
  <dd>
    The focus 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.</dd>
  <dt>
    range</dt>
  <dd>
    A range 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 Control 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="Properties" name="Properties">Properties</h2>
<dl>
  <dt>
    {{domxref("Selection/anchorNode","anchorNode")}}</dt>
  <dd>
    Returns the node in which the selection begins.</dd>
  <dt>
    {{domxref("Selection/anchorOffset","anchorOffset")}}</dt>
  <dd>
    Returns the number of characters that the selection's anchor is offset within the anchorNode.</dd>
  <dt>
    {{domxref("Selection/focusNode","focusNode")}}</dt>
  <dd>
    Returns the node in which the selection ends.</dd>
  <dt>
    {{domxref("Selection/focusOffset","focusOffset")}}</dt>
  <dd>
    Returns the number of characters that the selection's focus is offset within the focusNode.</dd>
  <dt>
    {{domxref("Selection/isCollapsed","isCollapsed")}}</dt>
  <dd>
    Returns a Boolean indicating whether the selection's start and end points are at the same position.</dd>
  <dt>
    {{domxref("Selection/rangeCount","rangeCount")}}</dt>
  <dd>
    Returns the number of ranges in the selection.</dd>
</dl>
<h2 id="Methods" name="Methods">Methods</h2>
<dl>
  <dt>
    {{domxref("Selection/getRangeAt","getRangeAt")}}</dt>
  <dd>
    Returns a range object representing one of the ranges currently selected.</dd>
  <dt>
    {{domxref("Selection/collapse","collapse")}}</dt>
  <dd>
    Collapses the current selection to a single point.</dd>
  <dt>
    {{domxref("Selection/empty","empty")}}</dt>
  <dd>
    Empty the current selection.</dd>
  <dt>
    {{domxref("Selection/extend","extend")}}</dt>
  <dd>
    Moves the focus of the selection to a specified point.</dd>
  <dt>
    {{domxref("Selection/modify","modify")}} {{gecko_minversion_inline("2.0")}}</dt>
  <dd>
    Changes the current selection.</dd>
  <dt>
    {{domxref("Selection/collapseToStart","collapseToStart")}}</dt>
  <dd>
    Collapses the selection to the start of the first range in the selection.</dd>
  <dt>
    {{domxref("Selection/collapseToEnd","collapseToEnd")}}</dt>
  <dd>
    Collapses the selection to the end of the last range in the selection.</dd>
  <dt>
    {{domxref("Selection/selectAllChildren","selectAllChildren")}}</dt>
  <dd>
    Adds all the children of the specified node to the selection.</dd>
  <dt>
    {{domxref("Selection/addRange","addRange")}}</dt>
  <dd>
    A range object that will be added to the selection.</dd>
  <dt>
    {{domxref("Selection/removeRange","removeRange")}}</dt>
  <dd>
    Removes a range from the selection.</dd>
  <dt>
    {{domxref("Selection/removeAllRanges","removeAllRanges")}}</dt>
  <dd>
    Removes all ranges from the selection.</dd>
  <dt>
    {{domxref("Selection/deleteFromDocument","deleteFromDocument")}}</dt>
  <dd>
    Deletes the selection's content from the document.</dd>
  <dt>
    {{domxref("Selection/selectionLanguageChange","selectionLanguageChange")}}</dt>
  <dd>
    Modifies the cursor Bidi level after a change in keyboard direction.</dd>
  <dt>
    {{domxref("Selection/toString","toString")}}</dt>
  <dd>
    Returns a string currently being represented by the selection object, i.e. the currently selected text.</dd>
  <dt>
    {{domxref("Selection/containsNode","containsNode")}}</dt>
  <dd>
    Indicates if a certain node is part of the selection.</dd>
</dl>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li>{{domxref("window.getSelection")}}, {{domxref("document.getSelection")}}, <a href="/en-US/docs/DOM/range" title="dom/Range">Range</a></li>
  <li>HTML5 DOM Range <a class="external" href="http://html5.org/specs/dom-range.html#selection" title="http://html5.org/specs/dom-range.html#selection">Interface Selection</a></li>
  <li><a class="external" href="http://lxr.mozilla.org/mozilla/source/content/base/public/nsISelection.idl">IDL definition in Mozilla cross-reference</a></li>
</ul>
Revert to this revision