How to keep a copy of selection object and work on it

Viewed 24

Say I have selected the below content till the word "called":


And some more information including an element </u><i>called nothing</i> and also some more <b><i>extra stuff</i></b>. Ha ha ha.

I'm using this function the get the selected part of the first node:

sselection= document.getSelection();
var range;
function getFirstNode() {
    var ssselection= sselection;
    range= ssselection.getRangeAt(0);
    range.setStart(ssselection.anchorNode, ssselection.anchorOffset);
    range.setEnd(ssselection.anchorNode, ssselection.anchorNode.length);
    var startC= range.startContainer;
    return [range.commonAncestorContainer.parentElement, range.toString()];
}

This function seems to have two problems:

  1. If I select only "me more informati", it will still select the entire first node.
  2. After extracting part of the first node, it entirely resets the selection, thus preventing me from working further on the selection.

How can I fix my code to solve 1. and how can I make it such so that it also retains the selection after the editing?

0 Answers
Related