I have contentEditable element (can be p, div, ...) and I would like to get caret (cursor) position in it. I can normally achieve it with this piece of code:
var position = window.getSelection().getRangeAt(0).startOffset;
This works fine while the element contains just text. But when the element contains some HTML formatting, the returned position is relative to caret position within included HTML element.
Let's assume contents of contentEditable element is this:
AB<b>CD</b>EF
If caret is inside <b></b>
, let's say between C and D, the returned position with above code is 1 instead of 3 (counted from the begining of the contentEditable element's content)
Can anybody come up with solution to this ?