Does anybody have workable solution to get or set cursor position in contenteditable div with elements inside? What I going to do is to create twitter-like field where I will insert on @ sign pressed. Problem is that browsers are pretty buggy with contenteditable attribute, so I cant rely on them for insert. So my algorithm is:
- When @ pressed
- GetCursorPos
- get content of div as a string, split it to two halves
- Insert string with span between halves
- Do $(div).html(new_content) - cursor will be dropped to beginning
- move cursor to old_cursor + span's text length
Problem with 2 and 5. I checked following questions:
- Set cursor position on contentEditable <div>
- How to set caret(cursor) position in contenteditable element (div)?
- Set the caret position always to end in contenteditable div
- How to move cursor to end of contenteditable entity
- contenteditable, set caret at the end of the text (cross-browser)
And many more... There is NO workable solution (FF, Chrome, IE) for setCursorPos(elem, pos) - everywhere is move to the end or save and restore. Also I have getCursorPosition, but sometimes in chrome it gives incorrect results, so this function appreciated also!
Thanks a lot!