0

How can I retrieve the pixel position (X and Y coordinates) of the textcursor within a <textarea> relative to the <textarea> or the viewport using JavaScript?

In my environment, dojo 1.6 is available.

I have found solutions with jQuery achieving similar things, but unfortunately I don't have jQuery available.

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
  • I've just found http://stackoverflow.com/q/9012835/522479 but the accepted answer doesn't work in my browser (FF22) at least. – Cobra_Fast Aug 13 '13 at 12:45
  • http://stackoverflow.com/q/6953037/522479 is essentially the same thing. The accepted answer points towards third party solutions though. – Cobra_Fast Aug 13 '13 at 12:49
  • http://stackoverflow.com/q/128342/522479 is exactly what I need, but the accepted answer uses jQuery and some of it's material is lost because of external hosting. – Cobra_Fast Aug 13 '13 at 12:59
  • https://github.com/Codecademy/textarea-helper/blob/master/textarea-helper.js looks very promising but sadly it's jQuery, too. – Cobra_Fast Aug 13 '13 at 14:54

1 Answers1

1

The textarea-caret-position component is a dependency-free JS library that returns the top and left pixel coordinates of the text cursor (caret). Despite the name, it works for both <textarea>s and <input> elements.

Gabriel Grant
  • 5,415
  • 2
  • 32
  • 40