[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5533
  • Last Modified:

Select/Highlight Input Text?

I want to programmatically select all the text in an <input>, so that if the user begins typing, s/he is overwriting the text that was previously in that box.

How do I do this?  I've tried the ol'

   oElement.select();

to no avail.  Thanks!
0
bryker
Asked:
bryker
  • 4
  • 3
1 Solution
 
brykerAuthor Commented:
By the way, I'm doing this in a KeyDown() event handler.
0
 
Pravin AsarCommented:
<script language="javascript">
function AutoSelect (textbox) {
    if (textbox.createTextRange) {
        var oRange = textbox.createTextRange();
        oRange.moveStart("character", 0);
        oRange.moveEnd("character", textbox.value.length);
        oRange.select();
    } else if (textbox.setSelectionRange) {
        textbox.setSelectionRange(0, textbox.value.length);
    }
}
</script>
<form>
<input  onfocus="AutoSelect(this);"  >

Assign appropriate event handler.

0
 
brykerAuthor Commented:
Doesn't work for me.  I'm calling your method from my KeyDown() event handler.  The caret moves to the end of the text, and the textbox does indeed receive focus, but nothing is highlighted/selected.

Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZvonkoSystems architectCommented:
Do you ask for this:

<input type=text name="Comment" value="Enter comment here" onFocus="if(this.value==this.defaultValue)this.value='';">


0
 
brykerAuthor Commented:
No, I'm not looking for the default-textbox-value thing you show here.  Thanks, though.
0
 
ZvonkoSystems architectCommented:
What about this:

<input type=text name="Comment" value="Enter comment here" onFocus="this.select();">

0
 
ZvonkoSystems architectCommented:
Or if you want to force overwrite, then do the erase by script:

<input type=text name="Comment" value="Enter comment here" onFocus="this.value='';">

0
 
brykerAuthor Commented:
I figured out a solution.  I put in a call to setTimeout('SelectText', 50), and executed my .select() there inside that procedure.  Something about being in this keyDown() handler just jacks things up.

Pravin, I'm giving you the points, since your code still works when executed in that queued procedure that's called by setTimeout().

Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now