Textarea Copy To Clipboard Button

Textarea Copy To Clipboard Button
Code Snippet:Simple Copy to Clipboard from textarea
Author: MORHERO
Published: January 20, 2024
Last Updated: January 22, 2024
Downloads: 810
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a “Textarea Copy To Clipboard Button” feature. It allows you to copy text from a textarea with a button click. The button is styled with CSS and uses JavaScript to copy the textarea’s content to the clipboard when clicked.

It provides a convenient way for users to copy text from a text area to their clipboard with a single button click. This feature can be helpful for forms, code-sharing platforms, and content-rich websites.

How to Create Textarea Copy To Clipboard Button

1. First, add the HTML structure to your web page. You’ll need a textarea element and a container div for the copy button.

<div class="text-wrap">
  <textarea id="textA"></textarea>
  <div id="copyToClipboard-a" class="clipboard icon"></div>
</div>

2. Next, apply CSS styles to create an appealing user interface. Customize the appearance of the textarea and the copy button as desired. Here’s an example:

html, body {
  height: 100%; 
  width: 100%; 
  margin: 0;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
}


.text-wrap {
  position: relative;
  background-color: #eee;
  padding: 2em;
}

textarea {
  min-width: 20vw;
  max-width: 80vw;
  min-height: 200px;
  max-height: 80vh;
  padding: 0.8em 1em;
  font-size: 1em;
  
}
textarea::-webkit-scrollbar {
    width: 4px;
}
textarea::-webkit-scrollbar-track {
    background: #f1f1f1; 
}
textarea::-webkit-scrollbar-thumb {
    background: #888; 
}
textarea::-webkit-scrollbar-thumb:hover {
    background: #555; 
}

.clipboard.icon {
  position: absolute;
    top: 2.3em;
    right: 2.5em;
  margin-top: 4px;
  margin-left: 4px;
  width: 11px;
  height: 13px;
  border: solid 1px #333333;
  border-top: none;
  border-radius: 1px; 
  cursor: pointer;
}
.clipboard.icon:before {
  top: -1px;
  left: 2px;
  width: 5px;
  height: 1px;
  border: solid 1px #333333;
  border-radius: 1px; 
}
.clipboard.icon:after {
  width: 3px;
  height: 1px;
  background-color: #333333;
  box-shadow: 8px 0 0 0 #333333; 
}

.icon:before, .icon:after {
  content: '';
  position: absolute;
  display: block;
}

3. Now, add the JavaScript code that enables the “Copy to Clipboard” functionality. This code listens for a click event on the button and copies the content of the textarea to the clipboard.

document.getElementById('copyToClipboard-a').addEventListener('click', function() {
  
  var text = document.getElementById('textA');
  text.select();
  document.execCommand('copy');

})

Modify the CSS to match your website’s design or adjust the appearance of the textarea and the clipboard icon.

That’s all! hopefully, you have successfully created the copy to clipboard button on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X