Copy text to clipboard by clicking a button, using only vanilla javascript without Flash.
JavaScript HTML
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
demo
test Add success callback test. Sep 23, 2015
.gitignore Initial import. Jul 24, 2015
.travis.yml
LICENSE Initial import. Jul 24, 2015
README.md Add static property to detect browser support. Apr 26, 2016
bower.json
clipboard-button.js Add support for multiline copy. Jul 18, 2016
karma.conf.js Initial import. Jul 24, 2015
package.json Add support for multiline copy. Jul 18, 2016

README.md

clipboard-button

Build Status Test Coverage

Copy text to clipboard by clicking a button, using only vanilla javascript without Flash. This is using the new Selection API and Clipboard API available in the latest browsers.

Browser support: Chrome 43+, Firefox 41+, Opera 29+ and IE10+.

See the demo.

Install

Install using npm or bower:

$ npm install clipboard-button --save
$ bower install clipboard-button --save

clipboard-button has no dependencies.

Usage

Create a button on your page, and specify the text you want to copy in the data-clipboard-text attribute:

<button id="myButton" data-clipboard-text="textToCopy">Copy</button>

Initialize clipboard-button by specifying a target button, either by a selector string or element instance:

function success() {
    console.log('Copied!');
};

function fail(err) {
    console.error('Error!', err);
};

var cpb = clipboardButton('#myButton', success, fail);

You can supply optional callback methods to be called for the success and error event. The error function will be called with the error object as first argument.

To clean up all event handlers and resources, you can call cpb.destroy().

To detect whether the browser is supported you can check the static property clipboardButton.supported.