Let’s say we have a contenteditable div, and inside the div there is a ul
<div contenteditable="true">
<ul>
<li>Some text here</li>
<li>Some more te|<- CARET HERExt</li>
</ul>
</div>
I need to get the node that the caret is inside, as well as the caret’s index inside the innerText.
I have used the following code:
let selection = getSelection();
console.log(selection.focusNode);
console.log(selection.focusOffset);
Expected output:
<li>
12
Actual output:
<ul>
3
2
Answers
Thank you @ram singh for your answer. I adapted it slightly for my use - here is the code:
user below code