[Javascript] Finer points of text selection

Fred isitmeornot at hotmail.com
Thu Dec 11 22:04:06 CST 2003


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20031211/1de16dc6/attachment.htm>


More information about the Javascript mailing list