Javascript command / copy and paste Jquery (ctrl + c)

2

I simply want the Javascript command to copy and paste, regardless of whether it works in all browsers.

There is a similar question here. But it requires it to work on all browsers. > Cross-browser way to copy text to Clipboard

This other question does not meet my expectations because I literally need the ctrl c command and do not need to run cross browser. (because the system will be just for me)

OBS: Must be Javascript, no other languages.

    
asked by anonymous 05.07.2015 / 17:14

1 answer

4
  

Note: This solution is specific to Internet Explorer

You can do this through the function execCommand :

  • Create a range ( range ) and choose which part of your document will be covered by this range:

    var range = document.createRange();
    range.selectNode(divACopiar);
    
  • Place this interval in the window selection:

    window.getSelection().addRange(range);
    
  • Invoke the command copy :

    var sucesso = document.execCommand("copy");
    
  • When you do this, the browser will display a security alert, asking the user to give permission to your page to access the clipboard:

    Iftheuserallows,thefunctionwillreturntrueandthecontentsoftheselecteddivwillbeintheclipboard.

    Example in jsFiddle.

        
    05.07.2015 / 19:47