Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including clipboard-button with all npm packages installed. Try it out:

var clipboardButton = require("clipboard-button")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

clipboard-button v1.2.0

Copy text to clipboard by clicking a button, using only vanilla javascript without Flash.

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.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free