This question already has an answer here:
I was wondering if it is possible to find the exact location of the carret inside a textarea in pixels in relation to the entire page. For instance, if I have typed This is text
into my text box, I would like to know the pixes from the top left of the screen to the carot.
It would be in the form X: 200 Y: 100. This is so I can position a floating div. This needs to be done dynamically in javascript.
Thanks guys
Here's a plug-in for this: jQuery caret plugin
This "raw code" works at least in IE.
Using the code you can put your
<TEXTAREA>
where ever you want in page, and the<DIV id="db">
will follow it. Even despite of the scrolling position of the page. You can fix the position of<DIV>
by changing the literal numbers atd.style...6
-statements.Positioning of the
<DIV>
is not quite exact, but gets better when using fixed-width font in<TEXTAREA>
.There is an implemention: https://github.com/beviz/jquery-caret-position-getter, it can gets caret position in textarea(i.e.x,y)
Here is a simple mix of ideas from Caret position in pixels in an input type text (not a textarea) , Caret position in textarea, in characters from the start and document.getElementById vs jQuery $() to get the caret position in jQuery for an
<input>
element. It works when clicking inside it, but not when moving the caret using the arrows or typing.Instead of
var inputter = document.getElementById('someid')
we usevar inputter = $('#someid')[0];
Here is a FIDDLE.