Forum Discussion

BenKanspedos-8b's avatar
BenKanspedos-8b
Community Member
3 years ago

Why does this box appear when I do Javascript to copy text to clipboard?

I have some buttons to let user copy text to clipboard. But a weird thing happens. the text shows up on the upper right and it doesn't go away. 

It's a bit annoying.

How can I get rid of it?

Here the code I am using:

var player = GetPlayer();
var text = player.GetVar("SLMobile");
copyFunction (text);


function copyFunction(tt) {
 

  const copyText = tt;
  const textArea = document.createElement('textarea');
  textArea.textContent = copyText;
  document.body.append(textArea);
  textArea.select();
  document.execCommand("copy");
  }

 

 

  • You are doing this in your code. The line document.body.append(textArea) does that. It adds a textArea to the HTML.

  • Thank you Math. Removing that line did remove the box, but it also prevented it from copying to my clipboard. However, it helped me understand the problem better.

    I was able to get updated code from chatGPT which removes the box

    var player = GetPlayer();
    var text = player.GetVar("ChatOpening");
    copyFunction (text);

    function copyFunction(tt) {
      
      const copyText = tt;
      const textArea = document.createElement('textarea');
      textArea.textContent = copyText;
        document.body.append(textArea);
        textArea.select();
      document.execCommand("copy");
      document.body.removeChild(textArea);
      }