copy to clipboard

In this example below you will see how to do a copy to clipboard with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by rpandeyIn, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright rpandeyIn ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>copy to clipboard</title>
  
  
  
  
  
</head>

<body>

  <p>
  <textarea class="js-copytextarea">copy text</textarea>
  
  <textarea id="textbox"></textarea>
</p>

<p>
  <button class="js-textareacopybtn">copy to clipboard</button>
</p>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/rpandeyIn/copy-to-clipboard-vmGzjY */
var copyTextareaBtn = $('.js-textareacopybtn').on('click', function(event) {
  var copyTextarea = $('.js-copytextarea');
  copyTextarea.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');
  }
});

Comments