I am facing an issue where I need to insert a token after selecting from a context menu at the caret position inside a contenteditable div. This works fine when the caret position is within a single line.
However, in my case, the range offset value becomes 0 whenever other HTML tags appear, indicating that the line has changed. I am currently using two functions to retrieve the range, which I came across on Stack Overflow.
Any assistance or advice on how to solve this problem would be greatly appreciated!
function saveSelection() {
var range = null;
if (window.getSelection) {
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
return sel.getRangeAt(0);
}
} else if (document.selection && document.selection.createRange) {
return document.selection.createRange();
}
return null;
}