0

I have:

HTML code:

<div class="server-ip clearfix">
<p>
  <i class="ion-ios-world"></i>
  <span id="serv-1">play.minesuperior.com</span>
</p>
<a class="copy-action" href="#!" onclick="copyText('serv-1')">
    <span class="copy-text">
      <i class="ion-scissors"></i>
      Copy
    </span>
</a>
</div>

JS function:

function copyText(textToCopy) {
  var copyText = document.getElementById(textToCopy).textContent;
  console.log(copyText);

  copyText.select();
  document.execCommand("Copy");

  alert("Copied the text: " + copyText);
}

I get error:

Uncaught TypeError: copyText.select is not a function

Why? In function I pass param: play.minesuperior.com. I tryied in console.log put param function and I get my text.

Ivan Beldad
  • 1,957
  • 2
  • 16
  • 31
Jadasdas
  • 697
  • 4
  • 13
  • 32
  • 2
    `copyText` is a string. What would a `.select()` method do when called on a string? – Andreas Apr 28 '18 at 05:36
  • Possible duplicate of [How do I copy to the clipboard in JavaScript?](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript) – Toastrackenigma Apr 28 '18 at 09:00

1 Answers1

3

Try below code snippet,

function CopyToClipboard(containerid) {

    var range = document.createRange();
     range.selectNode(document.getElementById(containerid));
     window.getSelection().addRange(range);
     document.execCommand("copy");
     alert("text copied") ;
}
<div class="server-ip clearfix">
                <p>
                  <i class="ion-ios-world"></i>
                  <span id="serv-1">play.minesuperior.com</span>
                </p>
                <a class="copy-action" href="#!" onclick="CopyToClipboard('serv-1')">
                    <span class="copy-text">
                      <i class="ion-scissors"></i>
                      Copy
                    </span>
                </a>
  </div>
programtreasures
  • 4,104
  • 1
  • 7
  • 25