By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

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

P: n/a
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().moveStar t('character',
-1000000));
}
Sep 28 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ReGenesis0 wrote:
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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 28 '08 #2

P: n/a
SAM
Le 9/28/08 4:11 PM, ReGenesis0 a écrit :
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 don't know if ... but :
<http://www.quirksmode.org/js/findpos.html>

--
sm
Sep 28 '08 #3

P: n/a
On Sep 28, 9:59*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
ReGenesis0 wrote:
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.
*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
Sep 28 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.