Tuesday 19 November 2019

javascript - Find caret position in textarea in pixels




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


Answer



This "raw code" works at least in IE.



Using the code you can put your







Positioning of the

is not quite exact, but gets better when using fixed-width font in