A modern approach to copy text to clipboard. No Flash. No frameworks. Just 3kb gzipped
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.
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.
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.
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.
var clipboard = new Clipboard('.btn'); clipboard.on('success', function(e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); e.clearSelection(); }); clipboard.on('error', function(e) { console.error('Action:', e.action); console.error('Trigger:', e.trigger); });