Rudchyk
2/7/2018 - 10:12 AM

Сopy To Clipboard

// @ts-check
/**
* @param {string} elementId - The ID of the element to locate.
*/
function copyToClipboard(elementId) {
  var editable, readOnly, range, selection,
      input = document.getElementById(elementId);

  if (navigator.userAgent.match(/ipad|iphone/i)) {
    editable = input.contentEditable;
    readOnly = input.readOnly;

    input.contentEditable = true;
    input.readOnly = false;

    range = document.createRange();
    range.selectNodeContents(input);

    selection = window.getSelection();
    selection.removeAllRanges();
    selection.addRange(range);

    input.setSelectionRange(0, 999999);
    input.contentEditable = editable;
    input.readOnly = readOnly;
  } else {
    input.select();
  }

  document.execCommand('copy');
}