Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Positioning the cursor in an input field

 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not control the position of the cursor in a text field
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic