October 21, 2001 - Detecting a Selection | WebReference

October 21, 2001 - Detecting a Selection

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

Yehuda Shiran, Ph.D.
Doc JavaScript

Although Netscape Navigator 4.x and Netscape 6 cannot retrieve the text of a selection within a text or textarea field, they can respond to one. Both Netscape browsers feature the onselect event handler that executes JavaScript code when the user selects some of the text within a form's field. This event handler cannot be used for other elements on the page. Try entering text in the field below and then select a portion of it with a mouse movement:

Enter your text and select it:

Here is how the form above is defined:

<FORM>
Enter your text and select it: <INPUT TYPE="text" NAME="data" SIZE="30"
       onSelect="alert('text selected')">
</FORM>
The onselect event handler is also supported by Internet Explorer as an event handler of text and textarea fields. This event does not bubble like most other events in Internet Explorer. Events that do not bubble can only be handled on the individual object that fired the event, so you cannot capture select events at the document level. Try the above selection exercise in both Netscape 6 (or Navigator 4.x) and Internet Explorer. You can observe a noticeable difference. Netscape 6 (as well as Navigator 4.x) reacts to the event's completion, when the mouse button is being released. Internet Explorer, though, reacts to the event's initiation, when the mouse button is being pressed. See Column 10 (The Internet Explorer Event Model) for a complete discussion of the Internet Explorer event model, and Column 11 (The Cross Browser Event Model) for the cross-browser event model.