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

Filter Unique Values

Kiểu đối tượng Set đã được giới thiệu trong ES6 và cùng với ..., ‘spread’ operator, chúng ta có thể sử dụng nó để tạo một mảng mới chỉ với các giá trị duy nhất.

Convert Array-like to True Array

Convert Array-like to True Array

Ajax, call jQuery POST to node.js expressjs

Ajax, call jQuery POST to node.js expressjs

try/catch es10

An update for developer convience allows the use of try/catch without an explicit e Error reference in the catch call.

Await in a for loop

How to Await in a for loop