This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Positioning the cursor in an input field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Positioning the cursor in an input field" Watch "Positioning the cursor in an input field" New topic
Author

Positioning the cursor in an input field

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Is there a way to obtain the position of the cursor in an input field? Conversely, is there a way precisely position the cursor in an input field?

I am writing a mask function for an input field and it occurred to me that the user may want to use the arrow keys to go back and insert a missing character in the input field.

It is easy enough to trap arrow keys and allow the user to insert a new character but when he does and the input field value is reformatted, the cursor goes to the end of the field, i.e. after the last character.

What I would like to do is put the cursor back to its previous position so that the user may enter multiple characters at the insert point.

Any suggestions would be appreciated.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can not control the position of the cursor in a text field
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
In IE, there is a TextRange object, that you can use. Not sure, if there something like tatn in Gecko.
See if those links will help:

JavaScript FAQ - store cursor position

look for answer #5 here

carret(cursor) postition in a textarea as an integer
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Positioning the cursor in an input field
 
Similar Threads
JTextPane
html navigation using arrows
Searching
Cursor positioning
Stupid Sql*Plus unix question