JS: copyToClipboard

The following function is a handy util function that you can use to copy a string into clipboard.

function copyToClipboard(str) {
  const el = document.createElement('textarea');
  el.value = str;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.left = '-9999px';
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
};

Reference

  • Copying text to clipboard with JavaScript

你可能感兴趣的:(JS: copyToClipboard)