clipboard.js
:scissors: Modern copy to clipboard. No Flash. Just 3kb gzipped :clipboard:
clipboard.js is a tiny 3KB JavaScript library that adds copy-to-clipboard functionality to any webpage button using a single HTML attribute, no Flash, no jQuery, no dependencies required.
clipboard.js is a tiny JavaScript library that gives web developers a clean, straightforward way to add "copy to clipboard" functionality to any button or element on a webpage — without needing Flash, jQuery, or any other heavy dependency. It weighs just 3 kilobytes when compressed, making it essentially invisible in terms of page size.
The problem it solves is a surprisingly annoying one. For years, copying text programmatically in a browser required either Adobe Flash or complex workarounds. clipboard.js eliminates all of that by using modern browser APIs: it relies on the browser's built-in Selection API (to select the text) and execCommand (to copy it), which are supported across all modern browsers including Chrome, Firefox, Safari, and Edge.
Using it is deliberately simple. You attach a special data attribute to an HTML button — for example, data-clipboard-target pointing to the ID of a text input field — and clipboard.js handles the rest when the user clicks that button. You can also embed the text to copy directly in the button's attribute without needing a separate target element at all. The library uses event delegation internally, meaning even if you have hundreds of copy buttons on a page, only one event listener is registered — keeping memory usage low.
For developers who need more control, it also offers a JavaScript API where you can dynamically compute what text gets copied or what element is targeted. It fires success and error events so you can show "Copied!" feedback to the user in whatever style fits your design.
You would use clipboard.js whenever you are building a documentation site, a code editor, a dashboard, or any web interface where users frequently need to copy snippets of text, API keys, code examples, or URLs with a single click. It is a pure JavaScript library installable via npm or a CDN script tag, requiring no build setup.
Where it fits
- Add a 'Copy' button to every code snippet on a documentation site using a single HTML attribute and two lines of JavaScript.
- Let users copy API keys or shareable URLs from a dashboard with one click and show a 'Copied!' confirmation.
- Add copy-to-clipboard to hundreds of elements on a page with minimal memory use, thanks to single event listener delegation.