[Javascript] Finer points of text selection

Paul Novitski javascriptlist at dandemutande.org
Fri Dec 12 01:06:58 CST 2003


Try the select method, e.g.:

<input type="text" name="thingie" value="blah de blah">
<br>
<input type="button" value="Click me" onClick="javascript: 
document.all.thingie.select();">

Paul


At 08:04 PM 12/11/2003, you wrote:
>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?
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list