JSFiddle - React, Tailwind, and code Playground

by Krzysztof Niecikowski

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input  id='1' class="js-copytextarea noshow" value='Sample Text to Copy 1 '>
<button  class="js-textareacopybtn" data-id="1">COPY</button>
<br>    
<input  type='hidden'  id='2' class="js-copytextarea" value='Sample Text to Copy 2 '>
<button class="js-textareacopybtn" data-id="2">COPY</button>

CSS

.noshow {display:none;}

JavaScript

$('.js-textareacopybtn').on('click', function(event) {
  var copyTextarea = $(this).data('id');
  $('#' + copyTextarea)[0].select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }
});