title: Creating Dynamic button with click event in JavaScript
body: <p>How can I create a dynamic button with a click event with JavaScript?</p>
  
  <p>I tried this, but when I click the add button, an alert message show up! It's not what I want - I want to be able to click the button which is dynamically created.</p>
  
  <pre><code>&lt;script language="javascript"&gt;
      function add(type) {
          //Create an input type dynamically.   
          var element = document.createElement("input");
          //Assign different attributes to the element. 
          element.setAttribute("type", type);
          element.setAttribute("value", type);
          element.setAttribute("name", type);
          element.setAttribute("onclick", alert("blabla"));
  
          var foo = document.getElementById("fooBar");
          //Append the element in page (in span).  
          foo.appendChild(element);
  
      }
  &lt;/script&gt;
  </code></pre>