Tutorial

clipboard.js: Copy Text to the Clipboard

Published on June 23, 2016
Default avatar

By Alligator.io

clipboard.js: Copy Text to the Clipboard

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

clipboard.js lets you easily copy text to the clipboard. At 3kb gzipped and no need for Flash, it’s a great lightweight way to allow your visitors to copy bits of text and code on your site.

Installation

Install with npm:

  1. npm install clipboard --save

Or get the package from Github and place the clipboard.min.js file manually in your project folder.

Then include the JavaScript file before the closing </body> tag:

<script src="path/to/clipboard.min.js"></script>

Finally, you’ll want to instantiate it. In the following JQuery example we’re instantiating the object for all elements with a class of clipboard:

$( document ).ready(function() {
  var clipboard = new Clipboard('.clipboard');
});

Usage

To use it, either use the data-clipboard-text data attribute:

<button class="clipboard"
   data-clipboard-text="This will be copied">
  Copy this
</button>

Or use the data-clipboard-target data attribute:

<span id="copy">This text will be copied</span>


Handling errors

Handle success and error states easily with the success and error custom events:

$( document ).ready(function() {
  clipboard.on('success', function(e) {
    $(e.trigger).text("Copied!");
    e.clearSelection();
    setTimeout(function() {
      $(e.trigger).text("Copy");
    }, 2500);
  });


Note: Some browsers don’t support the execCommand API needed for clipboard.js to work properly. The text will still get selected, so a good workaround is to show a message telling the user to hit CTRL+C when an error event is triggered.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hello, any updates/solutions as execCommand is now deprecated?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel