Hello readers, Today in this blog you'll learn how to create Copy Text to Clipboard Button using HTML CSS & JavaScript. Previously I have shared a Working Calculator using HTML CSS & jQuery, now it's time to create copying to the clipboard Button using JavaScript.
What does 'Copied to Clipboard' mean? The clipboard is where content goes on your device and computer when you Copy something to Paste later. In Keeper, you can copy record information to the clipboard by tapping the clipboard icon next to the field that you wish to copy.
As you can see in the image, there are two textarea box and one copy button. When you click on that copy in clipboard button the upper texts will be selected and copied.
When you click on that button there is a small tooltip that will appear with sliding from the left side. That tooltip used to inform a user the text has been successfully copied.
If you're feeling difficulty to understand what I am saying. You can watch a full video tutorial on this program (Copy Text to Clipboard Button).
Video Tutorial of Copy to Clipboard Button in Javascript
I hope you understood the basic codes behind the creating of this program. As you have seen in the video, to create this program I used HTML CSS & Javascript. Using only HTML & CSS to create this program is impossible. I used HTML & CSS for styling elements like textarea, button, and tooltip. And, I used Javascript Document.execCommand('copy') to copy the contents.
If you like this program (Copy Text to Clipboard Button) and want to get source codes. You can easily get the source codes of this program. To get the source codes you just need to scroll down.
If you like this program (Copy Text to Clipboard Button) and want to get source codes. You can easily get the source codes of this program. To get the source codes you just need to scroll down.
Copy Text to Clipboard in HTML CSS & Javascript [Source Codes]
As always, before sharing the codes of this program (Copy Text to Clipboard Button). Let's a few talk about the main tags and codes of this program. In HTML File, first I created a <div> tag with the class name "container" and placed all other tags like <textarea>, <span> and <button> tag inside it. Inside <textarea> I written some text to copy.
In the CSS File, first I placed all elements at the center using a CSS grid property. Then I did basic styling to textarea and button. Using span selector I created a small tooltip to inform the user the content has been copied or not. Then I hide that tooltip and coded to show a tooltip when the button will be clicked.
In the Javascript File, first I called all HTML Tag using CSS Selector. Then I coded to select all content of textarea when the button triggered or clicked. After that, I wrote an if-else statement to copy all selected text. Inside if statement I used Javascript Document.execCommand('copy') to copy the contents and in else condition I shown an alert if the program failed to copy.
To create this program (Copy Text to Clipboard Button). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste these following codes in your file.
In the CSS File, first I placed all elements at the center using a CSS grid property. Then I did basic styling to textarea and button. Using span selector I created a small tooltip to inform the user the content has been copied or not. Then I hide that tooltip and coded to show a tooltip when the button will be clicked.
In the Javascript File, first I called all HTML Tag using CSS Selector. Then I coded to select all content of textarea when the button triggered or clicked. After that, I wrote an if-else statement to copy all selected text. Inside if statement I used Javascript Document.execCommand('copy') to copy the contents and in else condition I shown an alert if the program failed to copy.
To create this program (Copy Text to Clipboard Button). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste these following codes in your file.


Good code😄😉
ReplyDeleteThank you...Keep visiting!
DeleteThank You so much CodingNepal i'm gonna Keep Visiting !
ReplyDeleteGreat :)
DeleteNot working on my compiler, also the html code is not complete
ReplyDeleteYes due to multiple errors I have changed the tag name of textarea so please check html file.
Deletetooltip not working
ReplyDeleteYou may did some mistakes...Please check it again.
DeleteNot working :(
DeleteIt's working please check your codes
DeleteIs this code working with WordPress ?
ReplyDeleteYes it work...Just paste the following codes
DeleteI'm not sure why it's not working. I copy and pasted both sections, an index.html and a style.css file in the same directory at the same level. I did notice on your HTML section that you didn't close the BODY or HTML tags after your SCRIPT tags. But it still didn't work. It can't be my fault because I simply copy and pasted your simple html and your simple css files, but thank you for your efforts.
ReplyDeleteThis codes works in blogger
ReplyDeleteIf yes how it paste in to my post.only code or css file both
Its not working, the button can't select text and copy the text :(
ReplyDeletePost a Comment
We welcome relevant and respectful comments. Off-topic or spam comments may be removed.