views:

229

answers:

1

I have a form box that I want to be always selected. I was able to get it to select everything in the box when it is clicked (using onFocus="this.select()") but I want it to be selected 100% of the time. The text in the box will be always changing, so I tried using onChange="this.select()" but that didn't work. Here's what I have:

    <form>
    <input type="text" id="txt1" size="30" maxlength="1" 
onkeyup="showHint(this.value)" onFocus="this.select()" onBlur="this.select()" 
onChange="this.select()" value="Click here, then press a key"/>
    </form>

Basically I just tried to call everything in hopes that something would work, but it is still acting as if only onFocus="this.select()" is there. By the way, this is for controlling something via keyboard, which is why the maxlength is only 1. I want it to be always selected so that when new key are pressed, the last command will be changed without having to use backspace.

A: 

Is there a reason you aren't just using document keystroke detection? If you need the value to appear in the input field for some reason, once you detect the keystroke, you could fill the text box. This would be much simpler than trying to maintain focus on the field itself.

Zurahn
I tried using document.getElementById(id).focus(); and document.getElementById(id).select(); to keep whatever is in the input field selected, but I can only get it working when I use onClick, which means I have to click the field each time I change what is in the field. Any idea how I could keep the text in the field always selected no matter what?
aloishis89