How we can create a program for copy text or contents on a single click? Solution: JavaScript Copy To Clipboard Text With CSS, Copy On Click Program.
Maybe you have seen a copy to clipboard buttons on some websites. Like quotes, those websites have a dedicated button to copy the quotes easily on a single click. Yes, we also can create that program simply using JavaScript. But maybe the program will not work on the old versions of browsers.
Today you will learn to create Copy On Button Click Program. Basically, there is a text field with a line of text and below is a button to copy all the matters, when you click on the button then all text will copy in your clipboard. Not only with the text field, but you can also place the program anywhere according to your requirement.
So, Today I am sharing JavaScript Copy To Clipboard Text With CSS Layout. This is a pure and easy JS program for copying content from the webpage. Suppose you share source codes like me, then put this program then your users don’t have to select the codes first and copy it’s can be done in a single mouse click.
If you are thinking now how this program actually is, then see the preview given below.
Preview Of Copy On Click Program
See this video preview to getting an idea of how this clipboard program looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
JavaScript Copy To Clipboard Source Code
Before sharing source code, let’s talk about it. First, I have created two HTML text fields. One for texts and other is blank for test and paste the texts. Between two text fields, I have put a button to run the program. In every element, I have placed a unique ID for styling and fetch and modify by JS.
Now in the CSS file, I have placed all the elements on right places. I have done some works using CSS like font style, colors, margin, padding, etc. In this program JavaScript handling the main feature, there is a simple JS command for the copy. For copying contents used JavaScript document.execCommand( 'copy' ) (info) command.
This program is very simple there is nothing special to explain, you will understand easily after getting the codes. For creating the program you have to create 3 files. First for HTML, second for CSS, and third for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>JS Copy To Clipboard | Webdevtrick.com</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>JavaScript Copy To Clipboard</h1> <textarea id="copy-text" spellcheck="false">These are some demo texts for testing copy to clipboard program. Click the button given below to copy these texts.</textarea> <button id="copy" type="button">Copy to clipboard<span class="done" aria-hidden="true">Copied</span></button> <textarea id="empty-field" placeholder="Paste your copied texts here for testing...."></textarea> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/** Code By Webdevtrick ( https://webdevtrick.com ) **/ body { text-align: center; font-family: "Open Sans", Helvetica, Arial, sans-serif; color: #212121; line-height: 1.6; } .done { position: absolute; left: 0; top: 0; right: 0; text-align: center; opacity: 0; transform: translateY(-1em); color: #000; transition: all .500s; } .copied .done { opacity: 1; transform: translateY(-2em); } h1 { margin: 1.75em auto 1.25em; } textarea, button { font-size: 1em; font-family: "Open Sans", Helvetica, Arial, sans-serif; } textarea { display: block; width: 700px; max-width: 100%; height: 75px; margin: 2em auto 1.5em; background: #F2F2F6; border: 1px solid #ddd; padding: 10px 15px; resize: vertical; } [id="empty-field"] { margin-top: 3em; } textarea:focus { border-color: red; } button { position: relative; padding: 8px 20px; border: 0; font-size: 0.835em; text-transform: uppercase; letter-spacing: 0.125em; font-weight: bold; color: #FFF; background: #e60023; transition: background .275s; } button:hover, button:focus { background: #212121; } |
function.js
The final step, Create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Code By Webdevtrick ( https://webdevtrick.com ) var toCopy = document.getElementById( 'copy-text' ), btnCopy = document.getElementById( 'copy' ), paste = document.getElementById( 'empty-field' ); btnCopy.addEventListener( 'click', function(){ toCopy.select(); paste.value = ''; if ( document.execCommand( 'copy' ) ) { btnCopy.classList.add( 'copied' ); paste.focus(); var temp = setInterval( function(){ btnCopy.classList.remove( 'copied' ); clearInterval(temp); }, 600 ); } else { console.info( 'document.execCommand went wrong…' ) } return false; } ); |
That’s It. Now you have successfully created JavaScript Copy To Clipboard Text With CSS, Copy On Click Program. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
Nice code. Can you tell me which text editor you are using in wordpress. New or old
Classic
I want to open text file in java script and put print button and cancel button. Currently i am trying with zenity but it is not working properly. Any help will be motivative.Thanks in advanced.
Sure, I will post about this soon.
and put print button and cancel button. Currently i am trying with zenity but it is not working properly. Any help will be motivative.Thanks in advanced.
Very useful articles for my site design
I would like to put TWO buttons on the same page. Two test-areas to copy.
I’ve tried creating a separate style2.css and functions2.js, and in .php in the code for the second use renamed all the vars as …2!
Could you help? The first one works fine.
The second one never copies, and changes the focus to the first
well i have a simple solution for this you can try this here is the code
<–
Title of the document
Copy Textarea
1 Selcet
and copy
text
Copy Textarea
2 Selcet
and copy
text
.text {
width: 100%;
height: 50px;
}
copyTextBtn = document.querySelector(‘#copyTextBtn’);
copyTextBtn.addEventListener(‘click’, function(event) {
let copyTextarea = document.querySelector(‘#copytextarea’);
copyTextarea.focus();
copyTextarea.select();
try {
let successful = document.execCommand(‘copy’);
let msg = successful ? ‘successful’ : ‘unsuccessful’;
alert(‘Copy text command was ‘ + msg);
} catch(err) {
alert(‘Unable to copy’);
}
});
copyTextBtn = document.querySelector(‘#copyTextBtn1’);
copyTextBtn.addEventListener(‘click’, function(event) {
let copyTextarea = document.querySelector(‘#copytextarea1’);
copyTextarea.focus();
copyTextarea.select();
try {
let successful = document.execCommand(‘copy’);
let msg = successful ? ‘successful’ : ‘unsuccessful’;
alert(‘Copy text command was ‘ + msg);
} catch(err) {
alert(‘Unable to copy’);
}
});
–>
what to do if i need multiple text to copy with multiple buttons ?