JCursorPos

A jQuery plugin to get a textarea or inputs cursor x/y position.

View project onGitHub

Overview.

JCursorPos is a cross browser plugin to get the cursor x/y and top/left coordinates in a textarea or input element. To use, simply do the following:

$('textarea').jCursorPos({
      onChange: function(cursor) {
      
          // the source element where the cursor is active
          var el = $(this);
          
          // the x/y of the cursor relative to the document
          console.log(cursor.offset.left);
          console.log(cursor.offset.top);
          
          // the x/y of the cursor relative to the textarea
          console.log(cursor.position.left);
          console.log(cursor.position.top);

      }
});

The cursor position relative to the window will be returned, as well as the position of the cursor relative to the inside of the textarea or input element.

Demo

Start typing and the cursor coordinate position will be updated below:

  • Cursor Document X/Y pos: 0px / 0px
  • Cursor Source X/Y pos: 0px / 0px

How To Use?

An onChange callback is fired everytime the cursor moves, passing a cursor object as a function parameter. The cursor object contains the following properties:

offset top Y coordinate of the cursor from the document window
left X coordinate of the cursor from the document window
position top Y coordinate of the cursor from the source element
left X coordinate of the cursor from the source element

Author and Contributors

This plugin was created by @davidshariff. Feel free to fork and submit any improvements or enhancements through a pull request.

Support or Contact

Having trouble or found a bug? Create a ticket at the issue page on GitHub.