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

Feb 07, 2023

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");
  }

 

 

3 Replies
Ben Kanspedos

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);
  }