Packagedom.objects
Classpublic class Selection
InheritanceSelection Inheritance Object

Introduced in: DOM 0 

Selection is the class of the object returned by window.getSelection() and other methods. A selection object represents the ranges that the user has selected.

Note (IE): Internet Explorer uses the document.selection.createRange() to create a TextRange, which is fundamentally different from Mozilla's Selection and W3C's Range objects.

View the examples

See also

Quirksmode - Accessing the user selection


Public Properties
 PropertyDefined By
  DOM 0 anchorNode : Node
[read-only] Returns the node in which the selection begins.
Selection
  DOM 0 anchorOffset : Number
[read-only] Returns the number of characters that the selection's anchor is offset within the anchorNode.
Selection
  DOM 0 focusNode : Node
[read-only] Returns the node in which the selection ends.
Selection
  DOM 0 focusOffset : Number
[read-only] Returns the number of characters that the selection's focus is offset within the focusNode.
Selection
  DOM 0 isCollapsed : Boolean
[read-only] Returns a boolean indicating whether the selection's start and end points are at the same position.
Selection
  DOM 0 rangeCount : Number
[read-only] Returns the number of ranges in the selection.
Selection
Public Methods
 MethodDefined By
  
DOM 0 addRange(range:Range):void
Adds a range to the selection.
Selection
  
DOM 0 collapse(parentNode:Node, offset:Number):void
Collapses the current selection to a single point.
Selection
  
DOM 0 collapseToEnd():void
Moves the anchor of the selection to the same point as the focus.
Selection
  
DOM 0 collapseToStart():void
Moves the focus of the selection to the same point at the anchor.
Selection
  
DOM 0 containsNode(node:Node, partlyContained:Boolean):Boolean
Indicates if the node is part of the selection.
Selection
  
DOM 0 deleteFromDocument():void
Deletes the actual text being represented by a selection object from the document's DOM.
Selection
  
DOM 0 extend(parentNode:Node, offset:Number):void
Moves the focus of the selection to a specified point.
Selection
  
DOM 0 getRangeAt(index:Number):Range
Returns a range object representing one of the ranges currently selected.
Selection
  
DOM 0 removeAllRanges():void
Removes all ranges from the selection, leaving the anchorNode and focusNode properties equal to null and leaving nothing selected.
Selection
  
DOM 0 removeRange(range:Range):void
Removes a range from the selection.
Selection
  
DOM 0 selectAllChildren(parentNode:Node):void
Adds all the children of the specified node to the selection.
Selection
  
Undocumented
Selection
  
Returns a string currently being represented by the selection object, i.e.
Selection
Property Detail
DOM 0 anchorNodeproperty
anchorNode:Node  [read-only]

Introduced in: DOM 0 

Returns the node in which the selection begins.

Note: A user may make a selection from left to right (in document order) or right to left (reverse of document order). The anchor is where the user began the selection. This can be visualized by holding the Shift key and pressing the arrow keys on your keyboard. The selection's anchor does not move, but the selection's focus, the other end of the selection, does move.


Implementation
    public function get anchorNode():Node

See also

DOM 0 anchorOffsetproperty 
anchorOffset:Number  [read-only]

Introduced in: DOM 0 

Returns the number of characters that the selection's anchor is offset within the anchorNode.

Note: This number is zero-based. If the selection begins with the first character in the anchorNode, 0 is returned.


Implementation
    public function get anchorOffset():Number

See also

DOM 0 focusNodeproperty 
focusNode:Node  [read-only]

Introduced in: DOM 0 

Returns the node in which the selection ends.

Note: A user may make a selection from left to right (in document order) or right to left (reverse of document order). The focus is where the user ended the selection. This can be visualized by holding the Shift key and pressing the arrow keys on your keyboard to modify the current selection. The selection's focus moves, but the selection's anchor, the other end of the selection, does not move.


Implementation
    public function get focusNode():Node

See also

DOM 0 focusOffsetproperty 
focusOffset:Number  [read-only]

Introduced in: DOM 0 

Returns the number of characters that the selection's focus is offset within the focusNode.

Note: This number is zero-based. If the selection ends with the first character in the focusNode, 0 is returned.


Implementation
    public function get focusOffset():Number

See also

DOM 0 isCollapsedproperty 
isCollapsed:Boolean  [read-only]

Introduced in: DOM 0 

Returns a boolean indicating whether the selection's start and end points are at the same position.

Note: Even a collapsed selection may have a rangeCount greater than 0. sel.getRangeAt(0) may return a range that is also collapsed.


Implementation
    public function get isCollapsed():Boolean

See also

DOM 0 rangeCountproperty 
rangeCount:Number  [read-only]

Introduced in: DOM 0 

Returns the number of ranges in the selection.

Note: Before the user has clicked a freshly loaded page, the rangeCount is 0. A user can normally only select one range at a time, so the rangeCount will usually be 1. Scripting can be use to make the selection contain more than 1 range.


Implementation
    public function get rangeCount():Number

See also

Method Detail
DOM 0 addRange()method
public function addRange(range:Range):void

Introduced in: DOM 0 

Adds a range to the selection.

Parameters

range:Range — A range object that will be added to the selection.

See also


Example
         // Select all STRONG elements in an HTML document
         var strongs = document.getElementsByTagName("strong");
         var s = window.getSelection();
         if(s.rangeCount > 0) s.removeAllRanges();
         for(var i = 0; i < strongs.length; i++) {
           var range = document.createRange();
           range.selectNode(strongs[i]);
           s.addRange(range);
         }
DOM 0 collapse()method 
public function collapse(parentNode:Node, offset:Number):void

Introduced in: DOM 0 

Collapses the current selection to a single point. The document is not modified. If the content is focused and editable, the caret will blink there.

Parameters

parentNode:Node — The caret location will be within this node.
 
offset:Number
  • 0 - Collapses the selection from the anchor to the beginning of parentNode's text.
  • 1 - Collapses the selection from the anchor to the end of parentNode's text.

See also

DOM 0 collapseToEnd()method 
public function collapseToEnd():void

Introduced in: DOM 0 

Moves the anchor of the selection to the same point as the focus.

The focus does not move. If the content is focused and editable, the caret will blink there.

See also

DOM 0 collapseToStart()method 
public function collapseToStart():void

Introduced in: DOM 0 

Moves the focus of the selection to the same point at the anchor.

The anchor does not move. If the content is focused and editable, the caret will blink there.

See also

DOM 0 containsNode()method 
public function containsNode(node:Node, partlyContained:Boolean):Boolean

Introduced in: DOM 0 

Indicates if the node is part of the selection.

Parameters

node:Node — The node that is being looked for whether it is part of the selection
 
partlyContained:Boolean — When true, containsNode returns true when a part of the node is part of the selection. When false, containsNode only returns true when the entire node is part of the selection.

Returns
Boolean — If the node is part of the selection.

See also

DOM 0 deleteFromDocument()method 
public function deleteFromDocument():void

Introduced in: DOM 0 

Deletes the actual text being represented by a selection object from the document's DOM.

See also

DOM 0 extend()method 
public function extend(parentNode:Node, offset:Number):void

Introduced in: DOM 0 

Moves the focus of the selection to a specified point.

The anchor of the selection does not move. The selection will be from the anchor to the new focus regardless of direction.

Parameters

parentNode:Node — The node within which the focus will be moved.
 
offset:Number — The offset position within parentNode where the focus will be moved to.

See also

DOM 0 getRangeAt()method 
public function getRangeAt(index:Number):Range

Introduced in: DOM 0 

Returns a range object representing one of the ranges currently selected.

Parameters

index:Number — The zero-based index of the range to return. A negative number or a number greater than or equal to rangeCount will result in an error.

Returns
Range — The range object that will be returned.

See also


Example
         ranges = [];
         sel = window.getSelection();
         for(var i = 0; i < sel.rangeCount; i++) {
             ranges[i] = sel.getRangeAt(i);
         }
         // Each item in the ranges array is now 
         // a range object representing one of the 
         // ranges in the current selection
DOM 0 removeAllRanges()method 
public function removeAllRanges():void

Introduced in: DOM 0 

Removes all ranges from the selection, leaving the anchorNode and focusNode properties equal to null and leaving nothing selected.

See also

DOM 0 removeRange()method 
public function removeRange(range:Range):void

Introduced in: DOM 0 

Removes a range from the selection.

Parameters

range:Range — A range object that will be removed to the selection.

See also


Example
         // Programmaticaly, more than one range can be selected.  
         // This will remove all ranges except the first.
         s = window.getSelection();
         if(s.rangeCount > 1) {
           for(var i = 1; i < s.rangeCount; i++) {
             s.removeRange(s.getRangeAt(i));
           }
         }
DOM 0 selectAllChildren()method 
public function selectAllChildren(parentNode:Node):void

Introduced in: DOM 0 

Adds all the children of the specified node to the selection. Previous selection is lost.

Parameters

parentNode:Node — All children of parentNode will be selected. parentNode itself is not part of the selection.

See also

DOM 0 selectionLanguageChange()method 
public function selectionLanguageChange():void

Introduced in: DOM 0 

Undocumented

See also

DOM 0 toString()method 
public function toString():DOMString

Introduced in: DOM 0 

Returns a string currently being represented by the selection object, i.e. the currently selected text.

Note: In JavaScript, this method is called automatically when a function the selection object is passed to requires a string: alert(window.getSelection()) // What is called alert(window.getSelection().toString()) // What is actually being effectively called.

Returns
DOMString — The string representation of selection.

See also

Examples
Note the order of the branches: the Mozilla Selection should come first! The reason is that Opera supports both objects; if you use window.getSelection() to read out the user selection, Opera creates a Selection object, while if you use document.selection it creates a Text Range object.
     var userSelection;
     if (window.getSelection) {
         userSelection = window.getSelection();
     } else if (document.selection) { // should come last; Opera!
         userSelection = document.selection.createRange();
     }