clipboard-button
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
.