This is code for finding the caaret position:-
enter code here
getCaretPosition() {
if (window.getSelection && window.getSelection().getRangeAt) {
var range = window.getSelection().getRangeAt(0);
var selectedObj = window.getSelection();
var rangeCount = 0;
var childNodes = selectedObj.anchorNode.parentNode.childNodes;
for (var i = 0; i < childNodes.length; i++) {
if (childNodes[i] == selectedObj.anchorNode) {
break;
}
if (childNodes[i].outerHTML)
rangeCount += childNodes[i].outerHTML.replace(/ /g, "").length;
else if (childNodes[i].nodeType == 3) {
rangeCount += childNodes[i].textContent.replace(/ /g, "").length;
}
}
return range.startOffset + rangeCount;
}
return -1;
}
when 'this is the input hello caretc| is here' is my html for editable div, I expect the caret position 49 but actual caret position is 6.