Reputation: 498
I am using rangy to manually restore the caret to the position where it previously was before replacing the html of a contenteditable div.
This is the code to restore the caret to it's previous position:
let newText = "I love squares <a href=#> #and </a> circles";
sel = rangy.getSelection();
savedSel = sel.saveCharacterRanges(textField[0]);
textField.html(newText);
sel.restoreCharacterRanges(textField[0], savedSel);
Notice that the | indicates where the caret is at the moment. The caret will be placed back behind the hashtag (#and):
I love squares #and |
rectangles and circles
But I would like to replace the last line with code to place the caret on the next line after having restored the caret to it's previous position:
I love squares #and
|rectangles and circles
Upvotes: 4
Views: 107