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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.