Logo

dev-resources.site

for different kinds of informations.

Creating copy button with JS

Published at
10/15/2020
Categories
javascript
copy
html
Author
walternascimentobarroso
Categories
3 categories in total
javascript
open
copy
open
html
open
Author
23 person written this
walternascimentobarroso
open
Creating copy button with JS

[Clique aqui para ler em português]

It is quite common that we need to copy something on the screen, either to search later or just to save somewhere, and with that, we will create a quick function to copy text with JavaScript.

Code

First we will create the interface, we will do something simple, using only HTML.

<input id="input" type="text" />
<button id="execCopy"> execCopy </button>
<button id="clipboardCopy"> clipboardCopy </button>
Enter fullscreen mode Exit fullscreen mode

In the HTML structure, an input was created that will be the text to be copied, and we have two buttons, which will be used to demonstrate two ways to copy text with JavaScript.

// Type 1
document.getElementById('execCopy').addEventListener('click', execCopy);
function execCopy() {
  document.querySelector("#input").select();
  document.execCommand("copy");
}

// Type 2
document.getElementById('clipboardCopy').addEventListener('click', clipboardCopy);
async function clipboardCopy() {
  let text = document.querySelector("#input").value;
  await navigator.clipboard.writeText(text);
}
Enter fullscreen mode Exit fullscreen mode

In the first type we add an event to the button with id execCopy that calls a function of the same name, in this function we first select the text, and then we use the execCommand("copy"); function; this is a native function that executes the copy command.

In the second type we add an event to the button with id clipboardCopy that calls a function of the same name, in this function we first retrieve the input value and then we move to the clipboard function, a detail in this function is that to work correctly it is necessary to use async and await, or use then(), as it is a promise.

ready as simple as that.

Demo

See the complete project working below.

Youtube

If you prefer to watch, I see the development on youtube (video in PT-BR).


Thanks for reading!

If you have any questions, complaints or tips, you can leave them here in the comments. I will be happy to answer!

😊😊 See you! 😊😊

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: