1

I am using rangy selection method for highlighted text. Get highlighted and remove highlighted functionality are working fine for me but the problem is that i am unable to copy the highlighted text.

Here are my code.

var highlighter;
    var highlightTextToBeRemoved;

  rangy.init();
    highlighter = rangy.createHighlighter();
    highlighter.addClassApplier(rangy.createClassApplier("highlight", {
      ignoreWhiteSpace: true,
      tagNames: ["span", "a"]
    }));

    document.onclick = function(event) {
      event = event || window.event;
      var target = event.target || event.srcElement;
      highlightTextToBeRemoved = highlighter.getHighlightForElement(target);
    };

  $scope.highlightSelectedText = function () {
            highlighter.highlightSelection("highlight");           
        };

      $scope.removeHighlightFromSelectedText = function(event) {
        if (highlightTextToBeRemoved) {
        highlighter.removeHighlights( [highlightTextToBeRemoved] );
        highlightTextToBeRemoved='';
    }
}; 

So i want copy functionality on highlighted text.

Any Idea?

arun kamboj
  • 1,145
  • 4
  • 17
  • 48

1 Answers1

0

You can try this command after highlighted text select: document.execCommand('copy'); It will return boolean if text copied then true otherwise false.

Gitesh Purbia
  • 1,094
  • 1
  • 12
  • 26
  • I am unable to select highlighted text. because rangy doesn't allow me to select already highlighted text. – arun kamboj Nov 27 '17 at 05:23
  • hi, check this link for your solution. I am using the same for highlight text and restore it later. https://stackoverflow.com/questions/6240139/highlight-text-range-using-javascript.. This is answer s posted by rangy developer tim down. – Gitesh Purbia Nov 27 '17 at 05:25