Link to home
Start Free TrialLog in
Avatar of AUGUSTIN
AUGUSTIN

asked on

javascript: Get Cursor Position - HTML TextBox

Hi All,
         I would like to know if it is possible to get the cursor position within as html textbox. For example, if there is a textbox and the user types in 'ABC'. he moves the cursor position to 2nd Char. When he presses the next key, i would like to get the exact position of the cursor within the textbox. i need the character position of the cursor.

Im using Interner Explorer browser.

Thanks in advance,
Augustin Prasanna. J
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AUGUSTIN
AUGUSTIN

ASKER

Hi CobolDinosaur,

                         If im not mistaken, the code you had posted works only if some text (in the textbox) has been selected. i need to know the cursor position without any selection of the text.

Thanks,
Augustin
I was afraid of that.  I was hoping the click when they positioned would give a test range.  That is the only way I know to get the position within a field. For click and key events it returns zero so it won't help.  

You could capture the x/y coordinates of the cursor and do a bunch of calculating based left position of the field and estimated with of the character cells, but it would not be accurate or reliable because user preferences could screw it up; plus where they click is not necessarily where the cursor positions itself.

It looks like this may be one of those things that would be nice to have but not feasible with the existing technology... at least not with any reliability.

Cd&
I provided the nearest workaround available. Can't be done is the correct answer until I run across something to change that.

Cd&
very useful thanks