I am in need of a way to determine the position of a character in an input field based on mouse movement.
For example, if the input field contains the value 'abcde' and I hover my mouse over the character 'a', the position should be 1. Similarly, hovering over 'b' should give me 2, and so on for the rest of the characters.
I have found some examples that utilize functions like selectionStart, but they only work when the caret is already at a specific position within the field.
What I really need is a solution that can track the position of the character under the mouse pointer without requiring any click action on the input field.
I have been unable to find any information on how to convert mouse coordinates into corresponding character positions using JavaScript APIs. Any suggestions or ideas would be greatly appreciated.
Update:
My ultimate goal is to automatically move the caret as the user drags over a contenteditable div. I came across a similar question on Stack Overflow, but no satisfactory solution has been provided yet.
Link to caret auto-move: https://stackoverflow.com/questions/12397792/update-caret-position-of-input-and-insert-text-on-drop-event/53660415#53660415
I have also included another example to demonstrate the difference in behavior between native drag and jQuery drag. Currently, I am using jQuery draggable, which does not support automatic caret dragging.