# tinymce.dom.Selection

Namespace | tinymce.dom
Class | Selection

This class handles text and control selection it's an crossbrowser utility class. Consult the TinyMCE Wiki API for more details and examples on how to use this class.

#### Example

## Public Methods

Method | Defined By
--- | ---
Selection(dom:DOMUtils, win:Window, serializer:Serializer) Constructs a new selection instance. | Selection
collapse(to_start:Boolean):void Collapse the selection to start or end of range. | Selection
getBookmark(type:Number, normalized:Boolean):Object Returns a bookmark location for the current selection. | Selection
getContent(s:Object):String Returns the selected contents using the DOM serializer passed in to this class. | Selection
getEnd():Element Returns the end element of a selection range. | Selection
getNode():Element Returns the currently selected element or the common ancestor element for both start and end of the selection. | Selection
getRng(w3c:Boolean):Range Returns the browsers internal range object. | Selection
getSel():Selection Returns the browsers internal selection object. | Selection
getStart():Element Returns the start element of a selection range. | Selection
isCollapsed():Boolean Returns true/false if the selection range is collapsed or not. | Selection
moveToBookmark(bookmark:Object):Boolean Restores the selection to the specified bookmark. | Selection
select(node:Element, content:Boolean):Element Selects the specified element. | Selection
setContent(content:String, args:Object):void Sets the current selection to the specified content. | Selection
setNode(n:Element):Element Sets the current selection to the specified DOM element. | Selection
setRng(r:Range):void Changes the selection to the specified DOM range. | Selection

## Public Events

Event | Defined By
--- | ---
onBeforeGetContent(selection:Selection, args:Object) This event gets executed before contents is inserted into selection. | Selection
onBeforeSetContent(selection:Selection, args:Object) This event gets executed before contents is extracted from the selection. | Selection
onGetContent(selection:Selection, args:Object) This event gets executed when contents is extracted from the selection. | Selection
onSetContent(selection:Selection, args:Object) This event gets executed when contents is inserted into selection. | Selection

## Method details

### Selectionconstructor

`public function Selection(dom:DOMUtils, win:Window, serializer:Serializer)`
Constructs a new selection instance.

#### Parameters

dom:DOMUtils | DOMUtils object reference.
win:Window | Window to bind the selection object to.
serializer:Serializer | DOM serialization class to use for getContent.

### collapsemethod

`public function collapse(to_start:Boolean):void`
Collapse the selection to start or end of range.

#### Parameters

to_start:Boolean | Optional boolean state if to collapse to end or not. Defaults to start.

### getBookmarkmethod

`public function getBookmark(type:Number, normalized:Boolean):Object`
Returns a bookmark location for the current selection. This bookmark object can then be used to restore the selection after some content modification to the document.

#### Parameters

type:Number | Optional state if the bookmark should be simple or not. Default is complex.
normalized:Boolean | Optional state that enables you to get a position that it would be after normalization.

#### Returns

Object - Bookmark object, use moveToBookmark with this object to restore the selection.

#### Example

### getContentmethod

`public function getContent(s:Object):String`
Returns the selected contents using the DOM serializer passed in to this class.

#### Parameters

s:Object | Optional settings class with for example output format text or html.

#### Returns

String - Selected contents in for example HTML format.

#### Example

### getEndmethod

`public function getEnd():Element`
Returns the end element of a selection range. If the end is in a text node the parent element will be returned.

#### Returns

Element - End element of selection range.

### getNodemethod

`public function getNode():Element`
Returns the currently selected element or the common ancestor element for both start and end of the selection.

#### Returns

Element - Currently selected element or common ancestor element.

#### Example

### getRngmethod

`public function getRng(w3c:Boolean):Range`
Returns the browsers internal range object.

#### Parameters

w3c:Boolean | Forces a compatible W3C range on IE.

#### Returns

Range - Internal browser range object.

#### See Also

### getSelmethod

`public function getSel():Selection`
Returns the browsers internal selection object.

#### Returns

Selection - Internal browser selection object.

### getStartmethod

`public function getStart():Element`
Returns the start element of a selection range. If the start is in a text node the parent element will be returned.

#### Returns

Element - Start element of selection range.

### isCollapsedmethod

`public function isCollapsed():Boolean`
Returns true/false if the selection range is collapsed or not. Collapsed means if it's a caret or a larger selection.

#### Returns

Boolean - true/false state if the selection range is collapsed or not. Collapsed means if it's a caret or a larger selection.

### moveToBookmarkmethod

`public function moveToBookmark(bookmark:Object):Boolean`
Restores the selection to the specified bookmark.

#### Parameters

bookmark:Object | Bookmark to restore selection from.

#### Returns

Boolean - true/false if it was successful or not.

#### Example

### selectmethod

`public function select(node:Element, content:Boolean):Element`
Selects the specified element. This will place the start and end of the selection range around the element.

#### Parameters

node:Element | HMTL DOM element to select.
content:Boolean | Optional bool state if the contents should be selected or not on non IE browser.

#### Returns

Element - Selected element the same element as the one that got passed in.

#### Example

### setContentmethod

`public function setContent(content:String, args:Object):void`
Sets the current selection to the specified content. If any contents is selected it will be replaced with the contents passed in to this function. If there is no selection the contents will be inserted where the caret is placed in the editor/page.

#### Parameters

content:String | HTML contents to set could also be other formats depending on settings.
args:Object | Optional settings object with for example data format.

#### Example

### setNodemethod

`public function setNode(n:Element):Element`
Sets the current selection to the specified DOM element.

#### Parameters

n:Element | Element to set as the contents of the selection.

#### Returns

Element - Returns the element that got passed in.

#### Example

### setRngmethod

`public function setRng(r:Range):void`
Changes the selection to the specified DOM range.

#### Parameters

r:Range | Range to select.

## Event details

### onBeforeGetContentevent

`public event onBeforeGetContent(selection:Selection, args:Object)`
This event gets executed before contents is inserted into selection.

#### Parameters

selection:Selection | Selection object that fired the event.
args:Object | Contains things like the contents that will be inserted.

### onBeforeSetContentevent

`public event onBeforeSetContent(selection:Selection, args:Object)`
This event gets executed before contents is extracted from the selection.

#### Parameters

selection:Selection | Selection object that fired the event.
args:Object | Contains things like the contents that will be returned.

### onGetContentevent

`public event onGetContent(selection:Selection, args:Object)`
This event gets executed when contents is extracted from the selection.

#### Parameters

selection:Selection | Selection object that fired the event.
args:Object | Contains things like the contents that will be returned.

### onSetContentevent

`public event onSetContent(selection:Selection, args:Object)`
This event gets executed when contents is inserted into selection.

#### Parameters

selection:Selection | Selection object that fired the event.
args:Object | Contains things like the contents that will be inserted.