Javascript: Copy string to clipboard
I needed a modern way to copy a string to the clipboard in JavaScript. Claude.ai helped me come up with this:
// With async/await
async function copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
console.log('Copied to clipboard');
} catch (err) {
console.error('Failed to copy: ', err);
}
}
Then you simply call it with a string
copyToClipboard("Hello world");