JSFiddle - React, Tailwind, and code Playground

HTML

<body>
  Lorem Ipsum
  <textarea id="data">test copy</textarea>
  <button onclick="copyText()">
    Copy Text
  </button>
</body>

JavaScript

$(window).on("copy", function(e) {
  alert(document.getSelection().toString());
});

var copyText = function() {
  var txt = document.createElement("textarea");
  txt.textContent = $('#data').val();
  txt.style.position = "fixed";
  document.body.appendChild(txt);
  txt.select();
  try {
    var range = document.createRange();
    range.selectNodeContents(txt);
    window.getSelection().addRange(range);
    return document.execCommand("cut");
  } catch (ex) {
    return false;
  } finally {
    document.body.removeChild(txt);
  }
}