• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Positioning the cursor in an input field  RSS feed

 
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.
 
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
 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!