Skip to content

Documentation / @andrew_l/dom / copyTextToClipboard

Function: copyTextToClipboard() ​

copyTextToClipboard(text): Promise<boolean>

Copies the provided text to the system clipboard.

The function attempts to use the navigator.clipboard API if available. If not, it falls back to using a fake element approach to copy the text.

Parameters ​

text ​

string

The text to be copied to the clipboard.

Returns ​

Promise<boolean>

A promise that resolves to true if the text was successfully copied, or false if the operation failed.

Example ​

ts
copyTextToClipboard('Hello, World!')
  .then(success => {
    if (success) {
      console.log('Text copied to clipboard!');
    } else {
      console.log('Failed to copy text.');
    }
  });