How to Copy Text to the Clipboard using JavaScript
A simpler way to copy with the Clipboard API

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Search for a command to run...
A simpler way to copy with the Clipboard API

Welcome to my blog ππ½
I'm a Front-End Developer with a passion for learning!
I write about Programming π©π½βπ» and Productivity Tips β
Wow, bookmarked! I will definitely use this method going forward.
Thanks for writing about this, Alyssa Holland.
Although my experience is primarily with front-end development, my current job is affording me the opportunity to do more full-stack work. Naturally, this meant that I needed to explore the different database GUIs available on the market. Here is a l...

A Note on the Series: Fast Fridays π is a series where you will find fast, short, and sweet tips/hacks that you may or may not be aware of. I will try to provide these (fairly) regularly on Fridays. In this Fast Friday tip, I'll explain how to crea...

Over the past year, Iβve started incorporating a Git GUI into my workflow. I still reach for the terminal about 90% of the time, but Iβve found that a GUI can really shine when it comes to things like viewing diffs, checking stashes, or resolving mer...

Simplifying Branch Management with Git Worktree

With all the AI tools popping up these days, I figured I'd try out some speech-to-text software in an attempt to boost my productivity. I've been hearing a lot about βdeveloping at the speed of thoughtβ, so here's a list of the top speech-to-text sof...

Copy buttons are a convenient tool that many websites and applications provide. The traditional way to do this wasn't too difficult, but it required some knowledge in some of the more uncommon WEB APIs and was a more imperative way to complete the task.
However, with the increase in browser support of the Clipboard API, there's now an easier, more declarative way to achieve this functionality. In this article, I will discuss both techniques so you can have options if you need to implement this feature on your own.
To appreciate the improved way of copying, we need to take a brief look at how we used to have to do things.
function copy(textToCopy) {
// Add a temporary textarea element that we can later run the
//"copy" command on.
const textarea = document.createElement("textarea");
//Add value we want to copy to the textarea
textarea.value = textToCopy;
//Append textarea to the document
document.body.appendChild(textarea);
//select the text to be copied
textarea.select();
// Copy the selected text inside the textarea to the clipboard
document.execCommand("copy");
//Remove temporary textarea from the document
textarea.remove();
}
As you can see it's a very imperative approach and, in my opinion, seems like a lot of work to perform a relatively common task. Now let's take a look at the improved (and simpler!) way to do this utilizing the Clipboard API.
The Clipboard API is a relatively new-ish feature that provides a convenient way to hook into common clipboard actions like copying, cutting, and pasting. This API is intended to replace the need of the document.execCommand() method which is now being deprecated. The awesome part about the Clipboard API is that it allows you to leverage Promises and provides a more succinct way to copy to the clipboard than we had before.
function copy(textToCopy) {
navigator.clipboard.writeText(textToCopy)
.then(() => {
/*Copying to clipboard was successful*/
})
.catch(() => {
//Error: Unable to copy to clipboard;
})
}
βοΈThis code block with the copy button in the upper right-hand corner is a great example of this type of functionality in the real world.
As you can see, this example is a much simpler and more declarative approach than the previous code snippet. The real magic is in the clipboard.writeText() method which takes the string that is passed in and copies it to the system's keyboard. If the copy was successful, the Promise will resolve, however, if the system does not have permission to write to the clipboard then it will be rejected.
Thanks to the increasing robustness of JavaScript we now have a better option when it comes to implementing this very handy feature of allowing the user to conveniently copy a piece of text to the clipboard.
If you enjoy what you read, feel free to like this article or subscribe to my newsletter, where I write about programming and productivity tips.
As always, thank you for reading, and happy coding!
Photo by Bethany Cirlincione on Unsplash