This is such a little handy function copy using Javascript copy to clipboard that allows you to create a button that will copy a block of content into the user's clipboard. The solution is to use the built-in execCommand function.
Table of contents: To start we need something that contains text like a JavaScript array. A very common scenario is a textarea element with a block of HTML (e.g. when you’re copying a Youtube embed video) for text to clipboard: With our HTML created, I’m going to use an addEventListener on the click event. Inside the click event we can then use the execCommand function as follows: The text inside the text area is now copied with the click event handler to the user’s clipboard to paste at their leisure after this user interaction. The application is now open using the JavaScript browser. This feature was implemented with document.execCommand(). Unfortunately some problems have arisen and the new API is now replaced. There is no browser support. In case you encounter an error, How to check for undefined in JavaScript so hopefully this will help. Here are a list of questions that I will attempt to answer in this article: Working on the clipboard extension transitions from document.execCommand method of Web API (discontinued) for older browsers to navigation.clipboard method. Notice that the navigator.clipboard API is a recent extension of the specification and depending on the browser you’re implementing it might take some time to implement. The readText() and writeText() methods in the Clipboard API’s are convenient choices for reading() and writing() methods. These are not supported on older browsers but definitely is for modern browsers. However you can copy a file or binary image to it. Copying uses blob data typically retrieved by fetch() or canvas.tolob() methods. This is passed in an object constructable in a clipboard and copied into a clipboard: Pasting is much harder, as multiple objects can return different types of contents. This requires iterating the various types until you have gotten the most useful format. Examples - see clipboard API’s demonstration images. The below examples are great to use in a visual environment with text areas and input elements on view. Sometimes the text can be copied to clipboards without display of input/textarea element. Here’s one example for workarounding these issues (essentially importing or copying something to the clipboard or removing a part) tested with Mozilla Firefox 4.1.11a1 and Internet Explorer 11.860017814. The links to these pages may not work. The API can be used in navigation.clipboard. Returns a real answer. This doesn’t necessarily mean that the browser can be used with every feature and hence a review must be conducted before using it. At this stage, Chrome supports its readText() method, but Firefox does not. As the browser supports the new Asymnc Clipboard API you may want to rely on document.execCommand(‘copy’). Below you will see an example code pen.io example might not work in this case read this note. Note the code isn’t working properly in StackOverflow embed previews. It is possible to request permission to use the clipboard by using permission based API of Chrome66. Added JavaScript Documents Support is gaining in popularity - see links to a list of updated browser links. Published on Jul 4, 2022 Tags: JavaScript
| execCommand
| clipboard
Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article
that you just finished reading.
No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner
or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in
and improve your skillset with any of the tutorials below.
Using execCommand to copy text into clipboard
<textarea id="embedcode">The text that you want to copy to clipboard goes here</textarea>
<button id="copytoclipboard" type="button">Copy</button>
document.querySelector('#copytoclipboard').addEventListener('click', function(event) {
var copyTextarea = document.querySelector('#embedcode');
copyTextarea.focus();
copyTextarea.select();
document.execCommand('copy');
});
Cut, Copy and Paste in JavaScript with the Clipboard API
Interact with the clipboard write permission
Copy and paste data with Clipboard write
Complex Example: Copy to clipboard without displaying input
Clipboard API feature detection
Async + Fallback
Async Clipboard API
Browser support
Related Posts
Tutorials
Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.