✂️ Modern copy to clipboard. No Flash. Just 3kb gzipped 📋 https://clipboardjs.com
Go to file
2015-09-21 10:32:11 -07:00
dist Removes "no-support" event in favor of "error" and "copy/cut" in favor of "success" 2015-09-21 10:32:11 -07:00
src Removes "no-support" event in favor of "error" and "copy/cut" in favor of "success" 2015-09-21 10:32:11 -07:00
test Only fire detailed events if copy was successful 2015-09-21 01:15:01 -07:00
.editorconfig Hello world 2015-09-17 23:15:24 -07:00
.gitignore Adds base website by using some bower dependencies 2015-09-19 00:18:33 -07:00
bower.json Updates headline and add default export 2015-09-21 00:37:02 -07:00
package.json Updates headline and add default export 2015-09-21 00:37:02 -07:00
README.md Removes "no-support" event in favor of "error" and "copy/cut" in favor of "success" 2015-09-21 10:32:11 -07:00

clipboard.js

A modern approach to copy & cut to the clipboard. No Flash. No dependencies. Just 2kb.

Demo

Install

You can get it on npm.

npm install clipboard --save

Or bower, too.

bower install clipboard --save

If you're not into package management, just download a ZIP file.

Setup

First, include the script located on the dist folder

<script src="dist/clipboard.min.js"></script>

Now, you need to instantiate it using a DOM selector. This selector corresponds to the trigger element, i.e. <button>.

new Clipboard('.btn');

Usage

We're living a declarative renaissance, that's why we decided to take advantage of HTML5 data attributes for better usability.

Copy text from attribute

The easiest way to copy some content to the clipboard, is to include a data-text attribute in your trigger element.

example-1

<!-- Trigger -->
<button class="btn" data-text="Heya!">Copy</button>

Copy text from another element

Alternatively, you can copy content from another element by adding a data-target attribute in your trigger element.

The value you include on this attribute needs to match another's element id attribute.

example-2

<!-- Target -->
<input id="foo" value="https://git.io/vn3cM">

<!-- Trigger -->
<button class="btn" data-target="foo">Copy</button>

Cut text from another element

Additionally, you can define a data-action attribute to specify if you want to either copy or cut content.

If you omit this attribute, copy will be used by default.

example-3

<!-- Target -->
<textarea id="bar">clipboard.js rocks!</textarea>

<!-- Trigger -->
<button class="btn" data-action="cut" data-target="bar">
    Copy
</button>

As you may expect, the cut action only works on <input> or <textarea> elements.

Events

There are cases where you'd like to capture what has been copied/cut or even check if there was an error.

That's why we fire custom events such as success and error for you to listen and implement your custom logic.

But to achieve that, first you need to access the triggers property from your clipboard instance.

var clipboard = new Clipboard('.btn');
var btns = clipboard.triggers;

Internally, this property is just a collections of nodes resulted from a querySelectorAll operation. So all you have to do now is loop through that collection and attach listeners.

for (var i = 0; i < btns.length; i++) {
    btns[i].addEventListener('success', function(e) {
        console.info('Action:', e.detail.action);
        console.info('Text:', e.detail.text);
    });

    btns[i].addEventListener('error', function(e) {
        alert(e.detail);
    });
}

Browser Support

This library relies on both Selection and execCommand APIs. When combined, they're supported in the following browsers.

Chrome logo Firefox logo Internet Explorer logo Opera logo Safari logo
42+ ✔ 41+ ✔ 9+ ✔ 29+ ✔ Nope ✘

License

MIT License © Zeno Rocha