1

I am looking for a way to copy the text inside of a header on my website to the users clipboard. I am a beginner at HTML and JS, so I am struggling quite a bit. This is what I have so far:

<!DOCTYPE html>
<html>
  <body>

    <h2>Test<h2 id="ip">

    <button onclick="copyToClipboard()">Copy To Clipboard</button>

    <script>

      function copyToClipboard() {
        var copyText = document.getElementById("ip")
        copyText.select();
        document.execCommand("copy");
      }

    </script>

  </body>
</html>

This script does not copy the header and I cannot figure out why. This is the error I get: Uncaught TypeError: copyText.select is not a function Any help would be appreciated.

Vadi
  • 3,279
  • 2
  • 13
  • 30
Mihkel
  • 689
  • 7
  • 34

1 Answers1

0

Your html is invalid. It should be <h2 id='ip'>Test</h2> Also I wouldn't advise to use inline onclick in the button element. Instead use the addEventListener function. Read here.

Ezrab_
  • 825
  • 5
  • 19
  • 44