I am trying to send the caret at the end of a contenteditable
, which works fine, except that it is not placed at the end if I insert an empty <span></span>
element and the end.
<div contenteditable>
<span>Start</span>
<span></span>
</div>
The Caret is placed at Start, but not on the new line.
This is the code I am using to place the caret at the end:
PlaceCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
Where el is the contenteditable div.
Thanks for your help.