Is there a total solution for getting a caret position and/or selection inside every browser from different elements. I’m looking for a solution witch I can execute like
mGetCaretPosition(iControl) witch will return the caret position inside it`s element.
I have tried a lot of functions:
selection (window/document) [document=IE, window=Opera]getSelection (window/document) [document=Firefox, document=Chrome, document=Safari]selectionStart (input/textarea) [All]craeteRange (selection)createTextRange (selection)
Calling a method like document.selection.createRange().text doesn’t return a caret position because it doesn’t have a selection. When setting tRange.moveStart(‘character’, -X) the X isn’t a known value. When you use this inside a div and the caret is in the middle it takes the code before the div.
I have build this today. It`s a combination of youre response alex and al the other results inside google. I have tested it inside the browsers IE9, Chrome, Opera, Safari and Firefox on the PC and also on a HTC Sensation with Android with the default browser, Firefox, Chrome and Opera.
Only the Opera on the mobile device did have some troubles.
My solution: