CodShot
JavaScript Public

Copy Text to Clipboard

A vanilla JavaScript helper to copy text to the clipboard with a fallback message.

#javascript #clipboard #utility #beginner
JavaScript
async function copyText(text) {
  try {
    await navigator.clipboard.writeText(text);
    console.log('Copied to clipboard');
    return true;
  } catch (error) {
    console.error('Clipboard copy failed:', error);
    return false;
  }
}

copyText('Hello from CodShot!');

Notes

The Clipboard API works only in secure contexts such as HTTPS or localhost.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-03 14:05
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support