How to copy text to clipboard in Javascript
By FoxLearn 2/27/2025 2:23:45 AM 33
Copying Text to Clipboard with JavaScript
Automatic clipboard copying can be risky, which is why most browsers make it difficult. No one wants their clipboard filled with suspicious links or unexpected content.
To ensure safety, any method for copying content to the clipboard must be triggered by a user action, such as a click. Even using Flash won’t bypass this requirement.
In this guide, we’ll walk you through easy methods to copy text to the clipboard using pure JavaScript.
1. Using document.execCommand()
This is an older method but still works in many browsers. It requires a hidden textarea or input element, and a user-triggered action (e.g., a click) to copy the text.
function copyTextToClipboard(text) { var textarea = document.createElement('textarea'); textarea.value = text; document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); document.body.removeChild(textarea); console.log('Text copied to clipboard'); }
You would call this function with the text you want to copy:
copyTextToClipboard('Hello, World!');
Remember, this method requires a user-triggered event (like a click), and the textarea must be visible for the function to work.
2. Using the Clipboard API
A more modern and recommended approach is using the Clipboard API
. This method is simpler and doesn’t require a hidden textarea.
function copyTextToClipboard(text) { navigator.clipboard.writeText(text).then(function() { console.log('Text successfully copied to clipboard'); }).catch(function(err) { console.error('Unable to copy text: ', err); }); }
You would use it the same way:
copyTextToClipboard('Hello, World!');
The Clipboard API is supported in most modern browsers and is a cleaner solution than using execCommand
.
3. Using Clipboard.js
If you're working with a library, Clipboard.js is a lightweight, dependency-free library designed for copying text to the clipboard without Flash.
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script>
Use it with a button:
<button class="btn" data-clipboard-text="This text will be copied">Copy text</button> <script> 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); }); </script>
In this example, all elements with the class .btn
will copy the specified text to the clipboard when clicked.
4. Fallback with Flash
If you need to support older browsers, Flash may be required as a fallback. Although this is not ideal, you can use ZeroClipboard, a Flash-based solution.
<!DOCTYPE html> <html> <head> <script src="ZeroClipboard.js"></script> </head> <body> <button id="copy-button" data-clipboard-text="Copy Me!" title="Click to copy me.">Copy to Clipboard</button> <script> var client = new ZeroClipboard(document.getElementById("copy-button")); client.on("ready", function(readyEvent) { client.on("aftercopy", function(event) { event.target.style.display = "none"; alert("Copied text to clipboard: " + event.data["text/plain"]); }); }); </script> </body> </html>
With ZeroClipboard, a click on the button will trigger the clipboard copy action. However, keep in mind that Flash's security restrictions mean that the clipboard can only be accessed when triggered by a user event, and not via JavaScript simulation.
For most use cases, You should be able to easily copy text to the clipboard in JavaScript. While the Flash-based solution is a fallback for legacy browsers, the modern execCommand()
and Clipboard.js approaches offer clean and efficient ways to achieve the same goal without relying on Flash.
- What does 'use strict;' means in Javascript
- How to detect if caps lock is pressed in Javascript
- How to create a Custom Event in Javascript
- How to Check if an Object Has a Property Properly in JavaScript
- How to convert an Uint8Array to string in Javascript
- How to Pass string parameter in an onclick function
- How to convert voice to text in Javascript
- LET vs VAR in JavaScript Variable Declarations