1

I'm a super novice--thanks in advance!

I'm looking to create a javascript button that can select everything from a multiple textareas and then copy the text to the clipboard when you click on a button.

I'm trying to share multiple video embed codes on one page and give the user a button to copy the embed code to the clipboard.

This answer from karoluS was helpful but I'm missing something to have multiple text areas on a page that the button can select the text from.

Copying text of textarea in clipboard when button is clicked

Can one button select from different text areas if I use different Ids?

libnov
  • 11
  • 1
  • 1
    Have you seen this question [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/q/400212). You can loop through your textareas and accumulate all values into a string, then use one of the methods described to copy the string to your clipboard – Nick Parsons Sep 02 '20 at 15:23

1 Answers1

1

Using the idea behind the answer you linked we can have anything copied to the clip board. By quickly making our own text area, adding the value you want to select, copying and then deleting the text area all behind the scenes like so.

function copyToClipboard (value) {
  const el = document.createElement('textarea');
  el.value = value;
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
};

Now you just need to work out what the value is you want to copy and how you get it. For example if you have multiple text boxes loop through them all and add their contents to one string, then call copyToClipboard on that string

DoubleJG
  • 85
  • 4