0

I made a copy and paste functionality and here it is

<script type="text/javascript">

function CopyToClipboard(containerid) {
    if (document.selection) {
      var range = document.body.createTextRange();
      range.moveToElementText(document.getElementById(containerid));
      range.select().createTextRange();
      document.execCommand("copy");

    } else if (window.getSelection) {
      var range = document.createRange();
      range.selectNode(document.getElementById(containerid));
      window.getSelection().addRange(range);
      document.execCommand("copy");
      alert("text copied, copy in the text-area")
    }
  }
</script>

and on my html

<strong>Referral Link: </strong>
<div id="div1"><?php echo $investors[0]['referal_code'];?></div>
<button class="btn" id="copy" onclick="CopyToClipboard('div1')"><i class="la la-copy"></i></button>

The problem is that I cant copy the value of the echo but when I tried to change what inside of my div like asdasd it copies successfully

why is that?

Rajan Sharma
  • 2,211
  • 3
  • 21
  • 33
Colline
  • 99
  • 10
  • 1
    What's the output of `var_dump($investors[0]);`? – Lukasz Formela Oct 15 '19 at 07:15
  • 1
    What is `asdasd` ? – executable Oct 15 '19 at 07:16
  • Is that div invisible? Or can you see the result of the `echo` call? –  Oct 15 '19 at 07:22
  • @LukaszFormela its dynamic value now the value is `zAsd21fg` – Colline Oct 15 '19 at 07:22
  • yes I can see the result of the echo call @ChrisG – Colline Oct 15 '19 at 07:23
  • @executable its just a sample data – Colline Oct 15 '19 at 07:23
  • What I wanted to know is what's the type of the `zAsd21fg` value - var_dump() would show you this. Is it just a string? – Lukasz Formela Oct 15 '19 at 07:24
  • The fact you echoed it should make no difference at all. By the time it reaches the browser it's just text. JavaScript doesn't know how it got there. Assuming the text is actually finding its way into the div then something doesn't quite add up – ADyson Oct 15 '19 at 07:24
  • Possible duplicate https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript – executable Oct 15 '19 at 07:25
  • @LukaszFormela yes it is just a string – Colline Oct 15 '19 at 07:28
  • The clipboard code works fine for me on Firefox. There's no error here. Whatever the div contains will end up in the clipboard. If it works with `asdasd`, then it will work with `zAsd21fg`. Double-check again please. –  Oct 15 '19 at 07:29
  • @ChrisG it works when i paste using mouse but not working when using ctrl + v – Colline Oct 15 '19 at 07:33
  • 1
    Where are you trying to paste it? Also, if mouse vs. Ctrl+V makes a difference, the question seems to be a very different one now, and completely unrelated to PHP. –  Oct 15 '19 at 07:35

0 Answers0