[Javascript] Finer points of text selection

Peter Brunone peter at brunone.com
Fri Dec 12 12:18:44 CST 2003


Fred,

    If you're working in IE only, look into the TextRange object; that
might help.

Cheers,

Peter

-----Original Message-----
From: javascript-bounces at LaTech.edu
[mailto:javascript-bounces at LaTech.edu] On Behalf Of Fred

I'm writing a masked edit textbox control for HTML (client-side script
only).  I have everything working okay except that I'm using the onkeyup
event to reformat the text.  In this case, I allow only numeric input,
but I format it as (nnn) nnn-nnnn for telephone numbers.  The problem
comes when I use shift and the left or right arrows to select a portion
of the text, and also when I first tab into the textbox and the entire
text is selected by default.  The onkeyup event formats the text
properly, but in the process it deselects the selected text.  I can try
to circumvent this by trapping for particular keystrokes, i.e. tab,
downshift, etc. and skipping the formatting.  But I would like to know,
for the record and for possible later needs, how to detect the selected
text and reselect it after formatting.  

In VB6 windows controls there are two properties, SelStart and
SelLength, that tell where the selected text begins and how long it is.
Does anyone know of a means of determining the same information
regarding an HTML input of type text?  I'm aware of the onselect event,
but I can't find any helpful properties associated with it.  Nor do the
event object or its srcElement have anything helpful in this regard.

Does anybody have a solution?





More information about the Javascript mailing list