title: Javascript copy text to clipboard without click any button on page load
body: <p>I've been trying to copy the Content of a my <strong>P tag</strong> to my clipboard without any button click.</p>
  
  <p>I tried it work on perfectly button click.Here is my working code for onClick.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
   &lt;meta charset="utf-8"&gt;
   &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
   &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;p id="p1"&gt;Copied..&lt;/p&gt;
   &lt;button onclick="copyToClipboard('#p1')" id="ashvin"&gt;Copy&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;script&gt;
   function copyToClipboard(element) {
    console.log('+++++++++++++++++++++++++++++++');
    var $temp = $("&lt;input&gt;");
    $("body").append($temp);
    $temp.val($(element).text()).select();
    var status = document.execCommand("copy");
    if(status){
     console.log("=======done")
    }else{
     console.log("=======error")
    }
    $temp.remove();
   }
  &lt;/script&gt;
  </code></pre>
  
  <p>It's working on button click fine, but i want to copy on page load not a click.</p>
  
  <p>Any help would be greatly appreciated. Thanks!</p>