JSFiddle - React, Tailwind, and code Playground

by Tushar Raj

HTML

<button>Copy</button>
<button>Paste</button>

CSS

button{
  width: 50px;
  height: 20px;
}

JavaScript

buttons = document.getElementsByTagName('button');

buttons[0].onclick = function(){
	//alert('copy')
  var textArea = document.createElement("textarea");
  textArea.style.position = 'fixed';
	    textArea.style.top = '100px';
	    textArea.style.left = 0;
	    textArea.style.width = '20em';
	    textArea.style.height = '20em';
	    textArea.value = 'abcd';
	    document.body.appendChild(textArea);
	    textArea.select();
      var successful = document.execCommand('copy');
	        var msg = successful ? 'successful' : 'unsuccessful';
	        console.log('copying text command was ' + msg);
      setTimeout(function(){
      	document.body.removeChild(textArea);
      },1000)
      
}

buttons[1].onclick = function(){
  var textArea = document.createElement("textarea");
  textArea.style.position = 'fixed';
	    textArea.style.top = '100px';
	    textArea.style.left = 0;
	    textArea.style.width = '20em';
	    textArea.style.height = '20em';
	    textArea.value = 'xyz';
	    document.body.appendChild(textArea);
      textArea.focus();textArea.select();
      setTimeout(function(){
      	var successful = document.execCommand('paste');
	        var msg = successful ? 'successful' : 'unsuccessful';
	        console.log('pasting text command was ' + msg);
      },1000)
      
}