Selection object (Object/browser)

An object representing a user selection in the current window.

Availability:

JScript - 3.0
Internet Explorer - 4.0
JavaScript syntax:IEmySelection = myDocument.selection
Object properties:type
Object methods:clear(), createRange(), empty()

This object represents a portion of the document in the current window that is currently highlighted, having been selected by the user or by a script. The selection is operated on by means of a TextRange object. This object is created by calling a createRange() method on the Selection object. This step is necessary because a selection cannot by its very nature persist very long, so a TextRange object encapsulates its value into a persistent store so it can be operated on, even though the original selection may have been deselected.

In Netscape Navigator, an entirely different technique is used that involves the document.getSelection() method.

Because it is easy to deselect the highlighted text by clicking on some other active object in the page, you will need to access the selection inside an event handler that is triggered by the selection action itself. This might be done quite effectively in an onSelectStart handler.

Warnings:

See also:Document.getSelection(), Document.selection, Password.select(), TextRange object

PropertyJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
type n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/aReadOnly.

MethodJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
clear() n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-
createRange() n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-
empty() n/a3.0 3.0 n/a4.0 4.0 n/a n/a n/a n/a n/a n/a-