I'm working on a way to create annotations in HTML text using JavaScript. The way it works, the user clicks on a contenteditable <div>
and I get the position of the cursor based on where they click. Then, when I'm placing the annotation in the text, I go to that character position to insert a footnote indicator. The problem is that the position of the cursor in the contenteditable <div>
doesn't take HTML tags into account. So for example, if the <div>
contained the following:
AB<b>CD</b>EF
And I placed the cursor between "C" and "D", the position is 3 because it ignores the <b>
. Is there a way to get the cursor position including HTML tags so that it is consistent when I go back and place my markers? The <div>
doesn't necessarily need to be contenteditable
if that opens up other solutions.
(I'm currently using this solution to get the position: Get a range's start and end offset's relative to its parent container)