Assuming a regular <input type=text> text-box with data in it.
Is it possible to detect (via JavaScript) the position of the text-coursor inside that text-box?
I am able to detect an ARROW LEFT or ARROW RIGHT keydown event – but how to detect the cursor location?
Why I need this:
I have a dynamic text-box here: http://vidasp.net/tinydemos/dynamic-textbox.html
It works great, however there are two scenarios that I would like to fix:
- when the cursor is at the beginning of the text-box and the user presses BACKSPACE
- when the cursor is at the end of the text-box and the user presses DELETE
(In both cases, the text-box must contain data for the effect to be observable.)
I’ve done quite a lot of work on this. The following works in all major browsers (including IE 6) for text
<input>s and<textarea>s and will work in all situations, including when there are leading and trailing spaces (which is where many solutions, including a-tools, fall down). There’s some background to this code in this question: IE's document.selection.createRange doesn't include leading or trailing blank linesYou can also get the following as part of a jQuery input/textarea selection plug-in I’ve written that is as yet undocumented: http://code.google.com/p/rangyinputs/