Copy the text to clipboard

Copy the text to clipboard

I got a variable value from the server. Originally it was from alter window so it is hard to copy/paste the text.
So I use the following code to retrieve it.
  1. function copyToClipboard(textToCopy){
  2.                 $("body")
  3.                     .append($('<input type="text" name="fname" class="textToCopyInput"/>' )
  4.                     .val(textToCopy))
  5.                     .find(".textToCopyInput")
  6.                     .select();
  7.                 try {
  8.                     var successful = document.execCommand('copy');
  9.                     var msg = successful ? 'successful' : 'unsuccessful';
  10.                     alert('The password has been copied to clipboard!');
  11.                 } catch (err) {
  12.                     window.prompt("To copy the text to clipboard: Ctrl+C, Enter", textToCopy);
  13.                 }
  14.                 $(".textToCopyInput").remove();
  15.             }
To use that I use the code

  1. var userKey = JSON.parse(response.responseText).UserKey;
  2.                                 copyToClipboard(userKey);

After I Ctrl-V, I got the text on the clipboard was 
 copyToClipboard


So help me please.