DEV Community

immodded
immodded

Posted on

How to create COPY TO CLIPBOARD BUTTON for url of webpage

<button onclick="copyURL()">Copy</button>

<script>
  function copyURL() {
    var currentURL = window.location.href;
    var tempInput = document.createElement("input");
    tempInput.value = currentURL;
    document.body.appendChild(tempInput);
    tempInput.select();
    tempInput.setSelectionRange(0, 99999); 
    document.execCommand("copy");
    document.body.removeChild(tempInput);
    alert("Copied!")
  }
</script>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)