How can I get the caret position from within an input field?
I have found a few bits and pieces via Google, but nothing bullet proof.
Basically something like a jQuery plugin would be ideal, so I could simply do
$("#myinput").caretPosition()
How can I get the caret position from within an input field?
I have found a few bits and pieces via Google, but nothing bullet proof.
Basically something like a jQuery plugin would be ideal, so I could simply do
$("#myinput").caretPosition()
VERY EASY
Updated answer
Use
selectionStart
, it is compatible with all major browsers.Update: This works only when no type is defined or type="text" on the input.
There are a few good answers posted here, but I think you can simplify your code and skip the check for
inputElement.selectionStart
support: it is not supported only on IE8 and earlier (see documentation) which represents less than 1% of the current browser usage.