how to copy text to clipboard in html code example

Example 1: javascript copy to clipboard

function copyToClipboard(text) {
  var input = document.body.appendChild(document.createElement("input"));
  input.value = text;
  input.focus();
  input.select();
  document.execCommand('copy');
  input.parentNode.removeChild(input);
}

Example 2: how to make a button that paste text in a textbox html

<!DOCTYPE html>
<html>
 <head>
   <script>
     function paste() {
            var pasteText = document.querySelector("#text");
            pasteText.focus();
            document.execCommand("paste");

            pasteText.value = pasteText.value + pasteText.value;
        }
   </script>
  </head>
  <body>
    <input id="text">
    <button onclick="paste()">Paste</button>
   </body>

Tags:

Html Example