Copy a string to the clipboard

tipjs,object

Copy a string to the clipboard. Only works as a result of user action (i.e. inside a click event listener).

const 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);
  const selected =
    document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
  if (selected) {
    document.getSelection().removeAllRanges();
    document.getSelection().addRange(selected);
  }
};
//Use
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.

Có thể bạn đã miss một số snippets code

closure javascript

closure javascript

Async await in javascript

Async await in javascript

Chuyển đổi thời gian trong javascript

Sử dụng Object.entries(), Array.prototype.filter() và Array.prototype.map(). Trả về định dạng ngày, giờ, phút với một miliseconds đã cho.

Sự khác biệt giữa substr và substring

Sự khác biệt giữa substr() và substring() chức năng trong JS là gì?

group by property javascript use reduce

group by property javascript use reduce