Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

Where is the cursor in a textbox

I know its possible to find out where the cursor is in a textbox however what Im looking to do is find the actual position of the cursor, so I can put a CSS div just below it, so I can put suggestions in for an intelisence feel.

My first idea was to calculate the text before, and add up each character spacing, and work out where lines would terminate etc, so I can work out the x&y position of the element, but surely there is an easier way of finding this out.

My other issue with my idea, is I have no idea where the scroll bar would be located, nor a way of detecting the scroll value, so it wouldn't work.

Any suggestions?

Thank you
Avatar of Amick
Amick
Flag of United States of America image

No need to work out the X,Y position. Look at this code:
http://dev-notes.com/code.php?q=33
Okay the link shows you how to get the cursor position.  For the rest of it:

Position of the scrollbar:

window.pageXOffset, window.pageYOffset  

IE equivalents are "document.body.scrollLeft" and "document.body.scrollTop"


position of the element:

document.getElementById('id').offsetTop
document.getElementById('id').offsetLeft

You just have to do the math to see where you are within the element
Avatar of tonelm54
tonelm54

ASKER

Amick -> Problem with that is it shows where the mouse is, but not where the cursor is.

COBOLdinosaur -> Which shows where the page is scrolled, but not the textbox :-(

The idea is that if Im writing in this comment box I can tell the x and y position if the | (text cursor), and then display a div underneath it.
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