Get caret XY coordinates in text area

orshachar picture orshachar · Feb 22, 2011 · Viewed 18.8k times · Source

Is there any good JQuery/JS solution to get the absolute position of a caret in text area (given in X/Y coordinates OR top/left coordinates).
The native Event object of keyDown JQuery function gives the x/y coordinates of the cursor and I want something similar the works on the working caret.

My goal is to add a floating HTML element the will be positioned relatively to the working caret position so I need to somehow get its position.



I need the solution to work on TextArea elements but a general solution to any editable html element would be even better!

Answer

Valentin Kantor picture Valentin Kantor · Feb 23, 2011

may be you want replace <textarea> with <div contenteditable="true">, it has almost the same behavior, and you can get coordinates by conventional way.