1

Currently i am writing an editor that is supposed to insert HTML at the caret position. This is supposed to happen after a choice has been made in the context menu.

Because opening the context menu removes the caret i'm saving it as following

this.range = caret.cloneRange();

After the choice has been made i'm restoring the caret position and inserting html as following

const sel = window.getSelection()!;
sel.removeAllRanges();
sel.addRange(this.range);
const node = document.createTextNode(text);
range!.insertNode(node);

Unfortunately this causes the following error

HierarchyRequestError: The operation would yield an incorrect node tree.

Is there any way i can save and restore the caret position and insert html after that?

sergenn
  • 61
  • 3
  • Does this answer your question? [Saving and Restoring caret position for contentEditable div](https://stackoverflow.com/questions/4576694/saving-and-restoring-caret-position-for-contenteditable-div) – Mark Schultheiss Sep 08 '20 at 11:12
  • There are other similar answers https://stackoverflow.com/q/1181700/125981 – Mark Schultheiss Sep 08 '20 at 11:14
  • If those or similar do not answer your question please provide more detail including HTML and JavaScript IN the question to provide more details and reproduce the problem right here. – Mark Schultheiss Sep 08 '20 at 11:16

0 Answers0