Loading...
// Select the text to copy
var textToCopy = "Hello, world!";

// Create a button element
var copyButton = document.createElement("button");
copyButton.textContent = "Copy to Clipboard";

// Add a click event listener to the button
copyButton.addEventListener("click", function() {
  // Create a temporary input element
  var tempInput = document.createElement("input");
  tempInput.value = textToCopy;

  // Append the input element to the document
  document.body.appendChild(tempInput);

  // Select the text in the input element
  tempInput.select();

  // Copy the selected text to the clipboard
  document.execCommand("copy");

  // Remove the input element from the document
  document.body.removeChild(tempInput);
});

// Add the button to the document
document.body.appendChild(copyButton);