Logo

dev-resources.site

for different kinds of informations.

Copying Items to Clipboard Without Using Clipboard API

Published at
1/13/2022
Categories
javascript
react
clipboard
copy
Author
ertanozdemir
Categories
4 categories in total
javascript
open
react
open
clipboard
open
copy
open
Author
12 person written this
ertanozdemir
open
Copying Items to Clipboard Without Using Clipboard API

There are several ways to use your clipboard. One of them is Clipboard API. This API is designed to supersede accessing the clipboard using document.execCommand() but if you have a web page that is served over HTTP, it probably won't copy anything to your clipboard. Because of security concerns, it works over HTTPS. In this blog post we will discuss how to achieve copying items without using Clipboard API.

Let's start

In this example we will use DOM operations and execCommand() method to create 'Copy to Clipboard' function. It shouldn't be forgotten that execCommand() method is deprecated and no longer recommended but some browsers still support it. Here the list of these browsers;
Supported Browser List


Our Code:



const handleCopy = (content) => {
  const textarea = document.createElement("textarea");
  textarea.textContent = content;
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand("copy");
  document.body.removeChild(textarea);
};


Enter fullscreen mode Exit fullscreen mode

Firstly we define handleCopy function. It has parameter named content. The content parameter is the value we want to copy.

1- We create a new text area.



document.createElement("textarea")


Enter fullscreen mode Exit fullscreen mode

2- And set it's textContent as the value we want to copy.



textarea.textContent = content;


Enter fullscreen mode Exit fullscreen mode

3- Then we add our text area to body of DOM.



 document.body.appendChild(textarea);


Enter fullscreen mode Exit fullscreen mode

4- Select all the text in textarea.



textarea.select();


Enter fullscreen mode Exit fullscreen mode

5- We use execCommand("copy") for copying the content that we selected.



document.execCommand("copy");


Enter fullscreen mode Exit fullscreen mode

6- Finally, we remove the textarea from DOM.



document.body.removeChild(textarea);


Enter fullscreen mode Exit fullscreen mode

You did it! Now the content is on your clipboard 🎉🎉

Conclusion

In this post, I showed you how to copy texts to your clipboard. I hope it helps to you.

See you soon 😋

copy Article's
30 articles in total
Favicon
Essential FFmpeg Recipes for Video Manipulation
Favicon
Unlocking Productivity: The Power of Keyboard Shortcuts
Favicon
Copy file between server and local machine ( from windows to linux server )
Favicon
Python's shutil module for Automated Testing
Favicon
The Pinnacle of Imitation: Master Copy Watches UAE Edition
Favicon
Two updates to the rucken copy-paste utility
Favicon
Docker Commands copy, remove images.
Favicon
How to load JSON data in PostgreSQL with the COPY command
Favicon
Looking forward experience developer
Favicon
OCP - OpenShift Container - Need to Copy Custom CA-Trust Certificates for Proxy Call from Company Domain to Azure
Favicon
Elevate Your Buttons with CopyShareify-js: Copy, Share, and More!
Favicon
Copying Files From Local To Remote Using PowerShell
Favicon
wayland & clipboard
Favicon
How to copy lots of data fast?
Favicon
Copy paste source files to destination with singular and plural replace text in file contents and file paths
Favicon
#010 Jira unformatted / formatted paste - How to paste plain text in Jira tickets | Jira messages - CTRL+SHIFT+V
Favicon
How to copy/paste files/directories into non-existent destination path
Favicon
Copy to Clipboard - Clipboard.js
Favicon
Copying Items to Clipboard Without Using Clipboard API
Favicon
Inspecting the Clipboard (on Linux)
Favicon
COPY progression in YugabyteDB
Favicon
HTML on the Clipboard: Oh, what shall we copy?
Favicon
Copy to Clipboard: The Options
Favicon
Copy to Clipboard: First Cut
Favicon
Bulk load into PostgreSQL / YugabyteDB - psycopg2
Favicon
Copy text to Clipboard using JavaScript
Favicon
How to Copy Array in Java
Favicon
[Dicas do VIM] Copiar, cortar e colar no VIM / NeoVim
Favicon
Creating copy button with JS
Favicon
Windows 10 copy multiple paste - shortcut/multiple paste option

Featured ones: