Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Created March 31, 2023 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeGlines/5966064a8bd28a92b611619e53b87eb1 to your computer and use it in GitHub Desktop.
Save JoeGlines/5966064a8bd28a92b611619e53b87eb1 to your computer and use it in GitHub Desktop.
// Get the text you want to save to the clipboard
const textToCopy = "This text will be saved to the clipboard.";
// Create a new textarea element to hold the text
const textarea = document.createElement("textarea");
textarea.value = textToCopy;
// Add the textarea element to the page
document.body.appendChild(textarea);
// Select the text in the textarea
textarea.select();
// Copy the selected text to the clipboard
document.execCommand("copy");
// Remove the textarea element from the page
document.body.removeChild(textarea);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment