JavaScript Copy To Clipboard

J

This is such a little handy function copy using Javascript copy to clipboard that allows you to create a button that will copy a block of content into the user’s clipboard. The solution is to use the built-in execCommand function.


Table of contents:

Using execCommand to copy text into clipboard

To start we need something that contains text like a JavaScript array. A very common scenario is a textarea element with a block of HTML (e.g. when you’re copying a Youtube embed video) for text to clipboard:

<textarea id="embedcode">The text that you want to copy to clipboard goes here</textarea>
<button id="copytoclipboard" type="button">Copy</button>

With our HTML created, I’m going to use an addEventListener on the click event. Inside the click event we can then use the execCommand function as follows:

document.querySelector('#copytoclipboard').addEventListener('click', function(event) {
var copyTextarea = document.querySelector('#embedcode');
copyTextarea.focus();
copyTextarea.select();
document.execCommand('copy');
});

The text inside the text area is now copied with the click event handler to the user’s clipboard to paste at their leisure after this user interaction.

Cut, Copy and Paste in JavaScript with the Clipboard API

The application is now open using the JavaScript browser. This feature was implemented with document.execCommand(). Unfortunately some problems have arisen and the new API is now replaced. There is no browser support.
In case you encounter an error, How to check for undefined in JavaScript so hopefully this will help.

Here are a list of questions that I will attempt to answer in this article:

  • Can Javascript copy to clipboard?
  • How do I copy text to clipboard?
  • How do I paste from clipboard in Javascript?
  • How do I copy something to the clipboard in HTML?
  • How do I copy to the clipboard in Javascript?
  • How do you copy text to clipboard on a button click?
  • Where is clipboard button?
  • How do I set copy to clipboard?
  • How do I show copied to clipboard?
  • Can Javascript access the clipboard?
  • How do you copy and paste text in Javascript?
  • How do you copy to clipboard in HTML?

Interact with the clipboard write permission

Working on the clipboard extension transitions from document.execCommand method of Web API (discontinued) for older browsers to navigation.clipboard method. Notice that the navigator.clipboard API is a recent extension of the specification and depending on the browser you’re implementing it might take some time to implement.

Copy and paste data with Clipboard write

The readText() and writeText() methods in the Clipboard API’s are convenient choices for reading() and writing() methods. These are not supported on older browsers but definitely is for modern browsers. However you can copy a file or binary image to it. Copying uses blob data typically retrieved by fetch() or canvas.tolob() methods. This is passed in an object constructable in a clipboard and copied into a clipboard: Pasting is much harder, as multiple objects can return different types of contents. This requires iterating the various types until you have gotten the most useful format. Examples – see clipboard API’s demonstration images.

Complex Example: Copy to clipboard without displaying input

The below examples are great to use in a visual environment with text areas and input elements on view. Sometimes the text can be copied to clipboards without display of input/textarea element. Here’s one example for workarounding these issues (essentially importing or copying something to the clipboard or removing a part) tested with Mozilla Firefox 4.1.11a1 and Internet Explorer 11.860017814. The links to these pages may not work.

Clipboard API feature detection

The API can be used in navigation.clipboard. Returns a real answer. This doesn’t necessarily mean that the browser can be used with every feature and hence a review must be conducted before using it. At this stage, Chrome supports its readText() method, but Firefox does not.

Async + Fallback

As the browser supports the new Asymnc Clipboard API you may want to rely on document.execCommand(‘copy’). Below you will see an example code pen.io example might not work in this case read this note. Note the code isn’t working properly in StackOverflow embed previews.

Async Clipboard API

It is possible to request permission to use the clipboard by using permission based API of Chrome66.

Browser support

Added JavaScript Documents Support is gaining in popularity – see links to a list of updated browser links.

About the author

By Jamie

My Books