5 out of 26 rated this helpful Rate this topic

selection Object

.NET Framework 3.0

Represents the active selection, which is a highlighted block of text or other elements in the document that a user or a script can carry out some action on.

Members Table

The following table lists the members exposed by the selection object.

Attributes/Properties

Property Description
type Retrieves the type of selection.
typeDetail Retrieves the name of the selection type.

Behaviors

Behavior Description
time2

Provides an active timeline for an HTML element or group of elements.

Collections

Collection Description
TextRange Retrieves a collection of TextRange objects.

Events

Event Property Description
ontimeerror

Fires whenever a time-specific error occurs, usually as a result of setting a property to an invalid value.

Methods

Method Description
clear Clears the contents of the selection.
createRange Creates a TextRange object from the current text selection, or a controlRange collection from a control selection.
createRangeCollection Creates a TextRange object collection from the current selection.
empty Cancels the current selection, sets the selection type to none, and sets the item property to null.

Prototypes

Object Description
Selection Constructor Defines the properties and methods inherited by objects in the Selection Constructor prototype chain.

Remarks

You can use the selection object as input from the user to identify which portion of the document to act on, or as output to the user to show the results of an action.

Users and scripts can create selections. Users create selections by dragging the mouse over a portion of the document. Scripts create selections by calling the select method on a text range or similar object. To get the active selection, apply the selection keyword to the document object. To carry out work on a selection, create a text range object from the selection using the createRange method.

A document can have only one selection at a time. The selection has a type that determines whether it is empty or contains a block of text or elements. Although an empty selection contains nothing, you can use it to mark a position in the document.

Standards Information

There is no public standard that applies to this object.

Applies To

document, HTMLDocument Constructor
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
tip - popup

Popups have document bodies too, And though popups do not receive focus, their text can yet be selected by mouse-scooping:

e.g.

PARENT: var popupobj=window.createPopup()
POPUP: [parent.popupobj.]document.selection.createRange().text

Note that this means there exist two selections accessible by the popup, --not just one:-- parent and parent.popupobj each has a selection.

bug or by design?

There's a bug or design issue when manipulating text using the selection object:
Objective: To manipulate the selected text within a textArea. Also, to insert text placeholders at the insertion point if the selection object is null.
Problem: The manipulation is fine when using the mouse. However, if I highlight using SHIFT+ARROW then tab to the apply button and press SPACE, the formatting is inserted outside of the textArea. I'm not sure how to counter this?
Code: Very basic testing code:

function applySelectedObjectTags(obj){
  

var age = txtAge.value;

// obtain the selection and pass it into a variable.

var txtSelection = document.selection;

// to manipulate the selected text you need to create a TextRange object.

var tRange = txtSelection.createRange();

var insertionText = tRange.text + " is " + age;

// You must check whether there is a selection or the formatting blocks will appear outside of the textarea

// You need to place the insertion at the point of contact and to then insert a placeholder: ie, ##

if(txtSelection == null)

{
alert("selection is nothing");

// Code to insert placeholder

return;

};

if(tRange != null)

{

tRange.text = insertionText;

return;

}


If you have any suggestions on whether this is by design or a bug, it would be appreciated; it's driving me nuts.


Esther Fan, MSFT: Thank you for your comments. For these kinds of questions, please try the following forums:
MSDN: http://forums.microsoft.com/msdn
TechNet: http://social.technet.microsoft.com/Forums/en-US/categories