6 out of 29 rated this helpful - Rate this topic

selection object

[This documentation is preliminary and is subject to change.]

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.

Standards information

There are no standards that apply here.

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.

 

 

Build date: 3/8/2012

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