function 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.length; else if (childNodes[i].nodeType == 3) { rangeCount += childNodes[i].textContent.length; } } return range.startOffset + rangeCount; } return -1; }
document.selection;
<div contenteditable="true"><p>one<br><p>t|wo</p></p></div>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)