Cursor position in textarea? (XY pos, not caret index)

R

ReGenesis0

Is it possible to determine the caret position, in terms of x/y
pixels, within a textarea? (I want to have a suggestion box pop up
under where you're typing... so i need to determine where you are
typing.)

-Derik

(OTOH, finding the Caret position-- between what characters the cursor
is positioned-- uses the function below. I include it to be nice to
future searchers, and because it took me an hour of frustrated
searching to find.)

function getCaretPos(el) {
if (typeof el.selectionStart != 'undefined')
return el.selectionStart;
else if (document.selection)
return
Math.abs(document.selection.createRange().moveStart('character',
-1000000));
}
 
T

Thomas 'PointedEars' Lahn

ReGenesis0 said:
Is it possible to determine the caret position, in terms of x/y
pixels, within a textarea? (I want to have a suggestion box pop up
under where you're typing... so i need to determine where you are
typing.)

I do not think it is generally possible to determine the coordinates
of the caret since you cannot always know which font is used.

I would be confused if the suggestion box would move while I am typing, so
it should suffice to calculate the offset position and height of the control.


PointedEars
 
R

ReGenesis0

I do not think it is generally possible to determine the coordinates
of the caret since you cannot always know which font is used.
*grumble*

I would be confused if the suggestion box would move while I am typing, so
it should suffice to calculate the offset position and height of the control.

It's a *large* textarea box. Placing the popup at top or bottom would
require scrolling the browser.

Is there a method for determining the rows then? Just hovering the
box below the current line would probably work...

-Derik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top