title: Onclick event in dynamically created button in javascript
body: <p>Requirements:
  I have one table with one row and three columns. first column is having button. when i click that button new row should be added to the table, and when i click the button in the newly added row(ie created dynamically) it should further add new row. </p>
  
  <p>only the last row button should add new row,all previous buttons should change to delete buttons . </p>
  
  <p>--java script function.</p>
  
  <pre><code>&lt;html&gt;
  
  &lt;head runat="server"&gt;&lt;TITLE&gt;Add/Remove dynamic rows in HTML table&lt;/TITLE&gt;
  
  &lt;SCRIPT language="javascript"&gt;
  
      function addRow(tableID) { 
          var table = document.getElementById(tableID);
  
          var rowCount = table.rows.length;
          var row = table.insertRow(rowCount);
  
          var cell1 = row.insertCell(0);
          var element1 = document.createElement("input");
          element1.setAttribute('type','button');
          element1.setAttribute('name','Add Row1');
          element1.setAttribute('value','Add Row');               
          cell1.appendChild(element1);     
  
          var cell2 = row.insertCell(1);
          var element2 = document.createElement("input");
          element2.type = "text";
          cell2.appendChild(element2);
  
          var cell3 = row.insertCell(2);
          var element3 = document.createElement("input");
          element3.type = "checkbox";
          cell3.appendChild(element3);
  
  
         element1.onclick=addRow(tableID);
  
      }
  &lt;/HEAD&gt;
  
  &lt;BODY&gt;
  
  &lt;TABLE id="dataTable" width="250px" border="1"&gt;
  
  &lt;TR&gt;
  
  &lt;td&gt;&lt;INPUT type="button" value="Add Row" name="Add Row" onclick="addRow('dataTable')"/&gt;&lt;/td&gt;
  
  &lt;TD&gt;&lt;INPUT type="text" /&gt;&lt;/TD&gt;
  
  &lt;TD&gt;&lt;INPUT type="checkbox" name="chk"/&gt;&lt;/TD&gt;
  
  &lt;/TR&gt;
  
  &lt;/TABLE&gt;
  &lt;/BODY&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>when i run this code i get one row with three columns. when i click the button in the first row.. its adding rows infinitely. please help me to get output .</p>
  
  <p>thanks in advance .</p>