Copy text from text area with Javascript

Home » Guides » Copy text from text area with Javascript

Copy text from text area with Javascript

Copy text made effortless with just a click!

Workflow is everything, and having a button to copy text from a textarea is much faster than clicking inside the textarea, selecting the text, and then copying it. This simple Javascript function turns a simple HTML button into an automatic copy machine.

1. Script Code

First, we need to add the relevant Javascript to the page's <head> tags. This is a simple function that we will call copytext.

<script>
function copytext() {
    let textarea = document.getElementById("textareaToCopyFrom");
    textarea.select();
    document.execCommand("copy");
}
</script>

2. HTML Code

Then add the relevant ID to the textarea itself, along with a button to execute the javascript function called copytext():

<textarea id="textareaToCopyFrom"></textarea>
<br>
<button onclick="copytext()">Copy to Clipboard</button>

That's it — we're done. Please let me know if it worked out for you!


Details

  • Title: Easy Javascript: Button to Copy Text from Textarea
  • Published:
  • Author:
  • Categories: JavaScript
Top Hosting Providers
Price

Want help?

Sometimes it can be hard to make a choice. Have you still not foun the information you're looking for, or are you wondering about something specific? Get in touch and we'll help you out.

Get in touch