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 ?
<p>
is contentEditable enabled. Now I am trying to solve problem when user wants to move from one paragraph to another just by using arrow keys. So I need to detect where in the paragraph is the caret, so I can reposition it according to pressed arrow key. – Alacrity