

JavaScript copy to clipboard function (no jQuery)
source link: https://dev.to/nickfrosty/javascript-copy-to-clipboard-function-no-jquery-19cb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JavaScript copy to clipboard function (no jQuery)
Recently, while working on my url shortener, I needed a JavaScript copy to clipboard function. No jQuery. Just vanilla JavaScript. I wanted to to be lightweight and use no jQuery (keeping it extra light, you know?) So after some researching and docs online, I came up with a pretty snazzy solution!
So my simple "copy to clipboard" function below is what I have come up with to solve this problem:
There is a full explanation of this JavaScript copy function below too :)
function copy(elem){
// get the text to be copied
if (elem.innerText != undefined)
text = elem.innerText;
else if (elem.value != undefined)
text = elem.value;
else
return false;
// create a temporary box, and stop the viewport scrolling
var box = document.createElement("textarea");
box.value = text;
document.body.appendChild(box);
box.style.top = "0";
box.style.left = "0";
box.style.position = "fixed";
// select the text in the box and copy it
box.focus();
box.select();
box.setSelectionRange(0, 99999);
document.execCommand("copy");
// alert the user of the copy?
alert("text copied to your clipboard!");
}
Don't forget to change the message or method of alerting the user you have copied to their clipboard. Unless you want this alert message I guess. That's cool too :)
Turns out: the clipboard is annoying...
This might surprise you, but the clipboard is really annoying to work with. It is really hard to actually to copy to clipboard in the browser. For a few reasons:
every browser is different
browsers really only want you to be able to copy text that is "selected"
For some reason, it seems like the browsers don't want to make it easy to manipulate the clipboard. But that is what hacked together code snippets are for!
How to clipboard in JavaScript
Add this JavaScript copy function into your web page. Put it anywhere you want, and it should work just fine.
On any text or element you want to copy text with JavaScript, use must call this "copy(this)" function. Like so:
Copy text from a text box or textarea:
<input type="text" name="text_box" value="copy to clipboard" onClick="copy(this);" />
<textarea cols="50" rows="4">try to copy me</textarea>
Copying text from a text box or text area proved to be super easy. The JavaScript "select" and "setSelectionRange" functions were literally designed for it! But... what about copying regular text on the page? A bit more tricky:
Copy text from a div, span, or any other html element:
<span class="my_class" onClick="copy(this);">copy this text</span>
When using non-user input text on a page, this copy function will only be able to copy the "inner text" of the element. So if you are trying to copy very specific text on the page, I suggest wrapping the text inside of some "span" tags. It makes getting the correct text much easier.
How this copy to clipboard function works:
This JavaScript function is pretty simple, but let me break it down:
First: using the parameter passed into the function, named param, I attempt to get the text/value to be copied. It performs a quick check to try to get the "innerText" or the "value". This check must be done since since most elements in the DOM will only return the actual text you want with one of these. For example, inputs and textareas will use "value" while elements like span and div will use "innerText".
if (elem.innerText != undefined)
text = elem.innerText;
else if (elem.value != undefined)
text = elem.value;
else
return false;
Next I create a fake element in the DOM to add the text we want to copy into it. I have to do this because in JavaScript, you can only "select" and copy text inside of an input or text area. So Ill just make a fake one not display it on the page.
Then I add some basic and specific styles to the fake textarea I created. You have to do this because as soon as we "select" the textarea to copy text with JavaScript, the browser will auto scroll to where the textarea is (which is at the bottom of the "body". So we make it fixed and at the top left so we don't see any scrolling. Wins!
// create a temporary box, and stop the viewport scrolling
var box = document.createElement("textarea");
box.value = text;
document.body.appendChild(box);
box.style.top = "0";
box.style.left = "0";
box.style.position = "fixed";
Lastly, we actually will "focus" on the fake textarea, "select" all of the contents, and actually execute the copy with JavaScript. This is effectively like clicking into the textarea, Ctrl+A, and Ctrl+C. You feel?
// select the text in the box and copy it
box.focus();
box.select();
box.setSelectionRange(0, 99999);
document.execCommand("copy");
So there we have it. The easiest and lightweight way to make a JavaScript copy to clipboard function. With no jQuery, just vanilla javascript. So light and wonderful!
Recommend
-
74
How to Add a Copy-to-Clipboard Button to Your Jekyll Blog I’m always looking for ways to improve my site’s user experience without toppling the precarious house of cards that is cross-browser compatibility. And one...
-
12
Quick Tip - Copy The Output Of The Last PowerShell Command To Clipboard I recently found myself poking around in PowerShell and going “oh, good now I want to copy and paste that output into an email/dialog box/tweet/note...
-
10
How to Add Copy to Clipboard Buttons to Code Blocks in Hugo March 22, 2019 · 989 words · ~5 minutes to read A small quality of life improvement for programming-related websites...
-
150
How to Add a Copy-to-Clipboard Button to Jekyll I’m always looking for ways to improve my site’s user experience without toppling the precarious house of cards that is cross-browser compatibility. And one thing that...
-
22
Copy from Remote Server to Local Clipboard via OSC 52 in Neovim2021-01-05Nvim264 words 2 mins read 10 times readIn my daily work, I usually log into a...
-
10
Build a Blazor 'Copy to Clipboard' component with a Markdown editor Let's build a 'Copy to Clipboard' component, where we can copy Mar...
-
4
Of course the user can just select the text and copy it but giving them a button that they can click to copy it seems like a better idea(better UX). So in this article I will show you how you can copy text to clipboard using JavaScript....
-
14
Mathys van der Merwe November 21, 2021 1 minute read ...
-
7
[JavaScript] Copy to Clipboard April 09, 2016 Copy content of
-
6
How to Copy Text and Images to Clipboard in Javascript? Jun 9, 2022 , by Agnidipta Bhattacharjee and
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK