Edit in JSFiddle

let copyBtn = document.getElementById('copyBtn');

copyBtn.addEventListener('click', () => {
  let copyTxt = document.getElementById('copyTxt');

  copyTxt.focus();
  copyTxt.select();

  document.execCommand('copy');
});
<input type="text" id="copyTxt" placeholder="Text to copy." />
<button id="copyBtn">Copy</button><br><br>
<textarea id="pasteHere" placeholder="Paste the copied text here." style="min-width: 220px;"></textarea>