Clipboard API

The Clipboard API provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard.

Accessing the clipboard

Instead of creating a Clipboard object through instantiation, you access the system clipboard through the Navigator.clipboard global:


Example 1:

readText (used for getting the copied text from the Clipboard)

navigator.clipboard
    .readText()
    .then(
        (clipText) => (document.querySelector(".editor").innerText += clipText),
    );

Example 2:

writeText (used for COPYING the text into the Clipboard)

function copyInputText(){
    const copyText = document.getElementById("myInput");
    navigator.clipboard.writeText(copyText.value);
}

Resources

Clipboard API (MDN docs),
Clipboard Events (W3Schools examples)