October 23, 2001 - Detecting a Selection | WebReference

October 23, 2001 - Detecting a Selection

Yehuda Shiran October 23, 2001
Detecting a Selection
Tips: October 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Internet Explorer 4.0x and above features the document.selection object which encapsulates a property and several methods that handle selections. The selection object provides information about the text and elements the user has currently highlighted with the mouse. This can be very useful if you want to perform some action based upon what the user has selected.

The selection object's type property defines the type of selection. This read-only string property can have one of the following values:

ValueDescription
"None"No selection or insertion point.
"Text"The selection is a text selection.
"Control"The selection is a control selection.

The default value is obviously "None", when there is no selection. Therefore, comparing the value of document.selection.type with "None" on Internet Explorer 4.0x and up, is equivalent to comparing the value returned by the document.getSelection() method with "" on Navigator 4.0x and Netscape 6.

Play around and select some lines. Press here to see the selection type.