clipboard.js wires copy and cut actions to buttons using data attributes or a small imperative API. It relies on modern Selection and execCommand APIs without Flash.
Overview
clipboard.js is a browser library that copies or cuts text from triggers you define with CSS selectors or DOM nodes. It targets pages that need a compact, dependency-free clipboard helper rather than a full UI framework.
Setup loads a built script from dist or a CDN, then instantiates ClipboardJS on selectors. Declarative data-clipboard-target, data-clipboard-text, and data-clipboard-action attributes bind triggers to source elements or inline strings. Cut actions apply only where the platform allows cutting from inputs or text areas.
Event delegation keeps memory use low when many matching elements exist. Success and error events let applications show feedback or fall back to keyboard hints on older browsers. An imperative API returns dynamic targets or text without changing markup, and destroy cleans listeners for single-page apps.
The library ships no CSS or tooltips; demos reference external styling. A separate browser extension adds copy buttons on popular documentation sites.
Key features
- Copy from elements or attributes
- Cut support where allowed
- Event delegation
- Success and error events
- Imperative target functions
- Modal container option
- Graceful older-browser fallback
- Support detection helper
Use cases
- Copy buttons on documentation sites
- Code block clipboard actions
- Forms copying generated values
- Dashboards sharing links or IDs
- SPAs with dynamic copy targets