tags:

views:

10320

answers:

5

For a project of mine I would love to provide auto completion for a specific textarea. Similar to how intellisense/omnicomplete works. For that however I have to find out the absolute cursor position so that I know where the DIV should appear.

Turns out: that's (nearly I hope) impossible to achieve. Does anyone has some neat ideas how to solve that problem?

A: 

Google is your friend: Floating Layer At Cursor Position http://www.willmaster.com/blog/css/floating-layer-at-cursor-position.php

Edit: Or this: http://www.webreference.com/programming/javascript/ncz/column2/

But I think I am missing some information. Is the problem that you cannot get the DIV to display over the textbox?

EDIT 2: Oops you want the cursor position in the textbox? Convoluted, but really GOOGLE IS YOUR FRIEND:

http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/

Kolten
This example deals with the mouse cursor position, but I believe the original poster meant the text cursor position, right?
PeterAllenWebb
Oh probably. I guess I really did mis-read it.
Kolten
Yes. the text cursor position was asked.
Armin Ronacher
A: 

This blog post seems to address your question, but unfortunately the author admits he has only tested it in IE 6.

Adam Bellaire
+10  A: 
enobrev
Nice, but I find it bugging in Chrome (2.0.172.37). To reproduce, first set the cursor on the top of textarea, then press 2 times pagedown so the textarea is scrolled and cursor moves to bottom. After this, the "Here I Am!" -label is always positioned too low (about two rows of text). Firefox seems to suffer from the very same syndrome, but it quickly fixes the position (div shows up in wrong place only once). What might be the trick here?
Tuukka Mustonen
recent google doc uses manually drawn cursor and calculated text position for texts...i wonder how they do it....
iamgopal
@iamgopal, how can i see that in action?
enobrev
I don't think they disclose much, but here is the related blogpost... http://googledocs.blogspot.com/2010/05/whats-different-about-new-google-docs.html
iamgopal
I see what you're saying. At least at the moment, when I open Google Docs and Chromium's developer tools, it looks like plain HTML. When trying to get a popup menu (like with spellcheck), it just puts a span around the word, and I figure they're grabbing the coordinates of the span.As for their explanation in that blog post, if they're explicitly positioning characters one by one (maybe with canvas?), then there's no doubt that it's set up so all the positioning information is easily attainable.
enobrev
A: 

I posted a topic related to this problem in Russian JavaScript related site.

If you don't understand Russian try translated by Google version: http://translate.google.ru/translate?js=y&prev=_t&hl=ru&ie=UTF-8&layout=1&eotf=1&u=http://javascript.ru/forum/events/7771-poluchit-koordinaty-kursora-v-tekstovom-pole-v-pikselyakh.html&sl=ru&tl=en

Thre is some markup issues in the code examples in translated version so you can check them in original one.

The idea is simple. There is no easy, universal and cross-browser method to get cursor position in pixels. Frankly speaking there is but only for Internet Explorer.

In other browsers if you do really need to calculate it you have to create invisible DIV, copy all styles and content of the text box into that DIV, then insert HTML element at exactly the same position in text where the caret is in the text box and get coordinates of that element.

prike
A: 

There is a specific implementation for BUEditor. http://ufku.com/drupal/bueditor/contributions/cursor-offset

drg.jon