### Install clipboard.js via npm Source: https://github.com/zenorocha/clipboard.js/blob/master/readme.md Use npm to install the clipboard.js package. This is the recommended method for projects using a package manager. ```bash npm install clipboard --save ``` -------------------------------- ### Initialize Clipboard.js for a Target Div Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/target-div.html Instantiate Clipboard.js by targeting elements with the class 'btn'. This setup is for copying content from a designated div. ```javascript var clipboard = new ClipboardJS('.btn'); ``` -------------------------------- ### Initialize Clipboard.js with NodeList Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-nodelist.html Selects all button elements on the page and initializes Clipboard.js to handle copying for all of them. This is useful for applying copy functionality to a group of elements. ```javascript var btns = document.querySelectorAll('button'); var clipboard = new ClipboardJS(btns); ``` -------------------------------- ### Initialize Clipboard.js with Target Textarea Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/target-textarea.html Initializes a new ClipboardJS instance targeting elements with the class 'btn'. It then sets up event listeners for 'success' and 'error' events to log details about the copy action. ```javascript var clipboard = new ClipboardJS('.btn'); clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` -------------------------------- ### Initialize Clipboard with Custom Text Function Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/function-text.html Use this to copy text dynamically generated by a function. The function should return the string to be copied. ```javascript var clipboard = new ClipboardJS('.btn', { text: function () { return 'to be or not to be'; }, }); ``` -------------------------------- ### Instantiate ClipboardJS with a selector Source: https://github.com/zenorocha/clipboard.js/blob/master/readme.md Initialize ClipboardJS by passing a CSS selector to the constructor. This will attach event listeners to all elements matching the selector. ```javascript new ClipboardJS('.btn'); ``` -------------------------------- ### Initialize Clipboard.js with a DOM Element Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-node.html Select a DOM element and pass it to the ClipboardJS constructor. Attach event listeners for 'success' and 'error' events to handle copy operations. ```javascript var btn = document.getElementById('btn'); var clipboard = new ClipboardJS(btn); clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` -------------------------------- ### Initialize Clipboard.js with Function Target Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/function-target.html Initializes Clipboard.js to copy content from a dynamically selected target element. The target function returns the DOM element to be copied. ```javascript var clipboard = new ClipboardJS('.btn', { target: function () { return document.querySelector('div'); }, }); ``` -------------------------------- ### Include clipboard.js via GitHack CDN Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers Use this script tag to include clipboard.js from GitHack, which is based on RawGit. This provides access to the library hosted on GitHub. ```html ``` -------------------------------- ### Include clipboard.js via RawGit CDN (Deprecated) Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers This script tag uses RawGit to include clipboard.js. Note that RawGit is deprecated and its use is not recommended for new projects. ```html ``` -------------------------------- ### Include clipboard.js via Statically CDN Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers Use this script tag to include clipboard.js from Statically, a CDN that hosts GitHub repository files. This provides a reliable way to access the library. ```html ``` -------------------------------- ### Include clipboard.js via unpkg CDN Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers This script tag includes clipboard.js from unpkg, a CDN that serves files from the npm registry. It allows versioned access to packages. ```html ``` -------------------------------- ### Clipboard.js Success and Error Event Handlers Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/function-target.html Sets up event listeners for 'success' and 'error' events on the Clipboard.js instance. These handlers log details about the copy action, the copied text, and the trigger element. ```javascript clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` -------------------------------- ### Include clipboard.js via jsDelivr CDN Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers Use this script tag to include the clipboard.js library from the jsDelivr CDN. This is a popular choice for hosting open-source JavaScript libraries. ```html ``` -------------------------------- ### Handle Copy/Cut Events Source: https://github.com/zenorocha/clipboard.js/blob/master/readme.md Listen for 'success' and 'error' events to provide user feedback or capture operation details. The 'success' event provides action, text, and trigger information, and allows clearing the selection. The 'error' event provides action and trigger information. ```javascript var clipboard = new ClipboardJS('.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); }); ``` -------------------------------- ### Copy from Input Field Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/target-input-number.html Initializes Clipboard.js to copy the value from an element with the class 'btn'. It includes event listeners for 'success' and 'error' events to log the action, copied text, trigger element, or error details. ```javascript var clipboard = new ClipboardJS('.btn'); clipboard.on('success', function(e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function(e) { console.log(e); }); ``` -------------------------------- ### Include clipboard.js via cdnjs CDN Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers Include the clipboard.js library using the cdnjs CDN. cdnjs is another widely used CDN for web assets. ```html ``` -------------------------------- ### Include clipboard.js script Source: https://github.com/zenorocha/clipboard.js/blob/master/readme.md Include the clipboard.js script in your HTML file. This can be done by referencing the file in the dist folder or using a CDN. ```html ``` -------------------------------- ### Programmatically Copy Text Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/text-programmatic-copy.html This snippet shows how to copy a hardcoded string ('123') when a button is clicked. It uses the `ClipboardJS.copy()` method and logs the success status to the console. ```html ``` -------------------------------- ### Include clipboard.js via Webcache CDN Source: https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers This script tag includes clipboard.js from Webcache, which utilizes cdnjs for serving the library. This option provides an alternative CDN source. ```html ``` -------------------------------- ### Handle Clipboard.js Success Event Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-nodelist.html Attaches an event listener to log details when a copy action is successful. It logs the action performed, the copied text, and the trigger element. ```javascript clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` -------------------------------- ### Clipboard.js Success and Error Event Handlers Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/target-div.html Attaches event listeners to handle successful copy operations and errors. Logs action details, copied text, and the trigger element to the console. ```javascript clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); clipboard.on('error', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` -------------------------------- ### Handle Copy Success Event Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/target-input.html Attach a success event listener to the clipboard instance. This function is called when the text is successfully copied to the clipboard, providing details about the action, copied text, and the trigger element. ```javascript clipboard.on('success', function (e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); }); ``` -------------------------------- ### Copy Text from Attribute Source: https://github.com/zenorocha/clipboard.js/blob/master/readme.md Use this snippet to copy a predefined text directly. The trigger element must have a `data-clipboard-text` attribute containing the text to be copied. ```html ``` -------------------------------- ### Programmatic Cut with Button Click Source: https://github.com/zenorocha/clipboard.js/blob/master/demo/target-programmatic-cut.html This snippet shows how to attach an event listener to a button. When clicked, it cuts the text from the element with the ID 'bar' and logs the result to the console. ```html