Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The writeText() method of the Clipboard interface writes the specified text to the system clipboard, returning a Promise that is resolved once the system clipboard has been updated.
writeText(newClipText)newClipTextThe string to be written to the clipboard.
A Promise that is resolved once the clipboard's contents have been updated.
NotAllowedError DOMExceptionThrown if writing to the clipboard is not allowed.
Writing to the clipboard can only be done in a secure context.
Additional security requirements are covered in the Security consideration section of the API overview topic.
This example sets the clipboard's contents to the string "<empty clipboard>".
button.addEventListener("click", () => writeClipboardText("<empty clipboard>"));
async function writeClipboardText(text) {
try {
await navigator.clipboard.writeText(text);
} catch (error) {
console.error(error.message);
}
}