DOM Reference Manual | All Packages | All Classes | Index | Frames | ||
Selection | Properties | Methods | Examples | ||
Package | dom.objects |
Class | public class Selection |
Inheritance | Selection Object |
Introduced in: | DOM 0 |
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.
See also
Property | Defined 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 |
Method | Defined By | ||
---|---|---|---|
Adds a range to the selection. | Selection | ||
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 | ||
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 | ||
Moves the focus of the selection to a specified point. | Selection | ||
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 | ||
Removes a range from the selection. | Selection | ||
Adds all the children of the specified node to the selection. | Selection | ||
DOM 0 selectionLanguageChange():void
Undocumented
| Selection | ||
Returns a string currently being represented by the selection object, i.e. | Selection |
DOM 0 anchorNode | property |
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.
public function get anchorNode():Node
See also
DOM 0 anchorOffset | property |
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.
public function get anchorOffset():Number
See also
DOM 0 focusNode | property |
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.
public function get focusNode():Node
See also
DOM 0 focusOffset | property |
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.
public function get focusOffset():Number
See also
DOM 0 isCollapsed | property |
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.
public function get isCollapsed():Boolean
See also
DOM 0 rangeCount | property |
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.
public function get rangeCount():Number
See also
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
// 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 —
|
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.
|
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.
|
Range — The range object that will be returned.
|
See also
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
// 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.
ReturnsDOMString — The string representation of selection.
|
See also
var userSelection; if (window.getSelection) { userSelection = window.getSelection(); } else if (document.selection) { // should come last; Opera! userSelection = document.selection.createRange(); }