title: Call a javascrtipt after button click
body: <p>I need to call a JavaScript after pressing a button with type button.</p>
  <pre><code>&lt;button type='button' 
          class='btn btn-sm btn-primary pull-right m-t-n-xs' 
          style='width: 100%;' id='demo' onclick='demo();'&gt;
            &lt;strong&gt;Edit&lt;/strong&gt;
  &lt;/button&gt;
  </code></pre>
  <p>I've tried the JavaScript below but it has errors</p>
  <pre><code> $('#demo').click(function() {
       alert('hey');
   });
  </code></pre>
  <p>For the JS above, it doesn't have an error but it doesn't alert..</p>
  <pre><code>document.getElementById(&quot;demo&quot;).onclick = function() { myFunction() };
  
  function myFunction() {
      alert('hey');
  }
  </code></pre>
  <p>The error for the js above is : Uncaught TypeError: Cannot set property 'onclick' of null at HTMLDocument.</p>
  <pre><code>window.onload = function() {
     document.getElementById(&quot;demo&quot;).onclick=function() {
       alert(&quot;Hello WOrld&quot;);
      }
  }
  </code></pre>
  <p>The error for this last one is:</p>
  <blockquote>
  <p>Uncaught TypeError: Cannot set property 'onclick' of null <br>
  at window.onload</p>
  </blockquote>