clear clipboard in javascript

index.tsx
function clearClipboard() {
  navigator.clipboard.writeText('')
    .then(() => {
      console.log('Clipboard cleared');
    })
    .catch(err => {
      console.error('Failed to clear clipboard: ', err);
    });
}
216 chars
10 lines

The clearClipboard() function uses the Clipboard API to write an empty string to the clipboard, effectively clearing it. If successful, the function logs a message to the console. If there is an error, it logs the error to the console. This function can be called whenever you need to clear the clipboard in your JavaScript code.

gistlibby LogSnag