jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes Auto focus on a text box after a key press Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Auto focus on a text box after a key press" Watch "Auto focus on a text box after a key press" New topic
Author

Auto focus on a text box after a key press

kul karni
Greenhorn

Joined: Nov 19, 2011
Posts: 12
Hi guyz,

I am looking for a technique that allows a textbox to be focussed automatically once a key is pressed on the keyboard.
More precisely, if I press a key on the keyboard, that character should appear automatically in that textbox with the cursor blinking.

For example, the main textbox of the Google Search Page, where the textbox starts capturing any key pressed.

Thank you.

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Google has focus in the textbox when the page loads.

Eric
kul karni
Greenhorn

Joined: Nov 19, 2011
Posts: 12
Thank you Eric. I agree with you on this.

But try to click on an empty space on the google page so that the textbox loses focus,
then type some characters and you will find those characters in that textbox even after removing the focus.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Listen for key presses



If it is in the range you want, focus the textbox document.getElementById("yourTextBox").focus();, just check to make sure

Eric
kul karni
Greenhorn

Joined: Nov 19, 2011
Posts: 12
Sorry Eric, I have no idea how to use this code. Please help me on this.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Auto focus on a text box after a key press