JSFiddle - React, Tailwind, and code Playground

JavaScript

let text = "123456";

let auxTextArea = document.createElement("textarea");
auxTextArea.value = text;

let copyTriggerButton = document.createElement("button");
copyTriggerButton.onclick = () => {
  let success = document.execCommand("copy");
  console.log(success);
};

document.body.appendChild(auxTextArea);
document.body.appendChild(copyTriggerButton);

auxTextArea.select();
copyTriggerButton.click();