copy to clipboard

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

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
Copyright rpandeyIn ©


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

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


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

  <button class="js-textareacopybtn">copy to clipboard</button>
  <script src=''></script>


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



/*Downloaded from */
var copyTextareaBtn = $('.js-textareacopybtn').on('click', function(event) {
  var copyTextarea = $('.js-copytextarea');;

  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');