0

I want to copy the text from function parameter, with other text after it

I tried making textarea element for only read and hide it,and put text in it but it didn't work

function Copy(str) {
  const AllWord = str + "<br><br>Copied From HERE";
  var el = document.createElement('textarea');
  el.value = AllWord;
  el.setAttribute('readonly', '');
  el.style = {
    position: 'absolute',
    left: '-9999px'
  };
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
}

Copy("CopiedText");

I expect it to copy "CopiedText"

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • 1
    corresponding HTML Code? – Monica Acha May 15 '19 at 18:26
  • I didn't write anything in the body section except that script –  May 15 '19 at 18:35
  • From the linked question: **Generally the page is required to be active (Async Clipboard API) or requires user interaction (e.g. a user click) to allow (document.execCommand('copy')) to access the clipboard see below for more detail.** – Barmar May 15 '19 at 18:56
  • So you can't just run the `Copy()` function from the top-level JavaScript, it has to be run from a user-initiated event. Put it in a button click listener. – Barmar May 15 '19 at 18:56

0 Answers0