Clipboard
Modern copy to clipboard. No Flash. Just 3kb gzipped
Clipboard.js
Copying text to the clipboard shouldn't be hard. It shouldn't require dozens of steps to configure or hundreds of KBs to load.
But most of all, it shouldn't depend on Flash or any bloated framework. That's why clipboard.js exists.
Copy text from another element
A pretty common use case is to copy content from another element. You can do that by adding a data-clipboard-target attribute in your trigger element.
Cut text from another element
Additionally, you can define a data-clipboard-action attribute to specify if you want to either copy or cut content.
If you omit this attribute, copy will be used by default.
As you may expect, the cut action only works on <input> or <textarea> elements.
Copy text from attribute
Truth is, you don't even need another element to copy its content from. You can just include a data-clipboard-text attribute in your trigger element.
Copy text from another element
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
That's why we fire custom events such as success and error for you to listen and implement your custom logic.
let clipboard = new Clipboard( ".btn" );
clipboard.on( "success", => (e) {
console.info( "Action : ", e.action );
console.info( "Text : ", e.text );
console.info( "Trigger : ", e.trigger );
e.clearSelection();
});
clipboard.on( "error", => (e) {
console.error( "Action : ", e.action );
console.error( "Trigger : ", e.trigger );
});