0

I cloned a node which contains a selection from a page with deep as true:

const selection = window.getSelection();
let node = selection.getRangeAt(0).commonAncestorContainer;
let clone = node.cloneNode(true);

But the selection is lost and there is no such a method as clone.getSelection(). I wonder how I can clone the selection from the node? Or how to simulate one on the clone? If it is possible, how can I get it from clone?

Lerner Zhang
  • 6,184
  • 2
  • 49
  • 66
  • Are you asking how you can "set" the selection on the cloned node, so that certain parts show _as_ selected, when you insert it into the document? https://stackoverflow.com/questions/4183401/can-you-set-and-or-change-the-user-s-text-selection-in-javascript – CBroe Apr 13 '23 at 10:25
  • I want to clone a node, and remove all elements with a specific tag name from the clone, and then get the selection offset from the clone. @CBroe – Lerner Zhang Apr 14 '23 at 03:19

0 Answers0