Search code examples
jquerytextareacursor-position

Cursor position in a textarea (character index, not x/y coordinates)


How can I get the caret's position in a textarea using jQuery? I'm looking for the cursor's offset from the start of the text, not for the (x, y) position.


Solution

  • Not jQuery, but just Javascript...

    var position = window.getSelection().getRangeAt(0).startOffset;