JSFiddle - React, Tailwind, and code Playground

copy to clipboard

by Trina Lu

HTML

<span ID="copytext" STYLE="height:150;width:162;background-color:pink">
This text will be copied onto the clipboard when you click the button below. Try it!
</span>
<textarea ID="holdtext" style="display:none;"></textarea>
<button>Copy to Clipboard</button>

JavaScript

var holdtext = document.querySelector('#holdtext');
var copytext = document.querySelector('#copytext');
document.querySelector('button').onclick = ClipBoard;

function ClipBoard() {
  holdtext.innerText = copytext.innerText;
  holdtext.style.display = "block";
  Copied = holdtext.select();
  var successful = document.execCommand('copy');
  holdtext.style.display = "none";
}