I have this bit of code that I use to get the cursor position in an editable div:
function getMeCurPos(element){
if (typeof window.getSelection != "undefined") {
var range = window.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset);
caretOffset = preCaretRange.toString().length;
return caretOffset;
}
}
The problem is that the caretOffset function returned only the amount of text content, not the html tags. For instance,
Consider this line in my editable div:
Hey <b>jony</b>, whats goin on in the | party
* The cursor is indicated by |.
Running getMeCurPos (ele) returns: 30but it should return 37. It does not count tagsb
source
share