Last active
September 13, 2023 08:26
-
-
Save yairEO/c6801315f59e6c3bc00b61d0ae7fe678 to your computer and use it in GitHub Desktop.
Get caret character offset at selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCaretCharOffset(elm) { | |
var caretOffset = 0; | |
if (window.getSelection) { | |
var range = window.getSelection().getRangeAt(0), | |
preCaretRange = range.cloneRange(); | |
preCaretRange.selectNodeContents(elm) | |
preCaretRange.setEnd(range.endContainer, range.endOffset) | |
caretOffset = preCaretRange.toString().length | |
} | |
return caretOffset | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this function don`t count \n or "<"br/">"