Copying text with the Clipboard() API

Activation code, share link, voucher... Copying content to the clipboard can come from many situations. Let's explore how to do it on the developer side with Vanilla JavaScript.

The problem with execCommand()

The old school solution would be to use execCommand(). For that, you need an editable element to copy the content from (which means either an input or an HTML element with contenteditable attribute). Set the focus to this element and execute the "Copy" command.

const element = document.querySelector('#element')

Paste the content somewhere and it should do the job. This solution works but has drawbacks:

The Clipboard() API

As you would guess, this is another way to copy content and it was indeed implemented to replace the use of execCommand() and to create a global and proper solution for clipboard interactions.

This API is asynchronous and thus returns a promise once called. Here is the basic implementation to copy text to the clipboard (note that this only works on pages secured by HTTPS):

  .then(() => {
    console.log(`"${text}" was copied to the clipboard.`)
  .catch(err => {
    console.error(`Error copying text to the clipboard: ${err}.`)

To get access to the clipboard's content and paste it, you first need the user's permission. Then it's pretty similar:

  .then(text => {
    console.log(`Pasted text: ${text}.`)
  .catch(err => {
    console.error(`Error pasting the clipboard's content: ${err}.`)

Compared to execCommand(), this solution is a bit longer but I find it easier to read and the asynchronous part makes it painless to perform other tasks until the promise is resolved. You can see a live version on CodePen.

There's no doubts that new features will be coming to this new API along with a better browser support which is quite bad for now. You can learn more about the Clipboard API here and about the included events with ClipboardEvent.