I ran the following: when I try to select the text in the element contenteditable, and the end of the selection - is the beginning of an element's content, then no selection event does not start, and there is Selection, and Rangeobjects.
Can someone please give me any advice on why this might happen or how I can prevent it?
Code responsible for obtaining the selection range:
$('div[contenteditable="true"]').bind("mouseup keyup touchend", function() {
lastCaretIndex = getSelectionRange();
});
function getSelectionRange() {
var sel;
if (window.getSelection) {
sel = window.getSelection();
console.log(sel);
if (sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (document.selection) {
return document.createRange();
}
return null;
}
<div id="main-input" contenteditable="true">Hello world!</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
Run codeHide resultJSFiddle (open your browser console to make sure the selection is not logged).

source
share