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.