title: Dynamically add/remove rows from html table
body: <p>I am working on a table that can be modified by pressing "Delete" buttons in each row and "Insert row" to add a new one at the end:</p>
  
  <p>So far by now my code is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  function deleteRow(id,row) {
      document.getElementById(id).deleteRow(row);
  }
  
  function insRow(id) {
      var filas = document.getElementById("myTable").rows.length;
      var x = document.getElementById(id).insertRow(filas);
      var y = x.insertCell(0);
      var z = x.insertCell(1);
      y.innerHTML = '&lt;input type="text" id="fname"&gt;';
      z.innerHTML ='&lt;button id="btn" name="btn" &gt; Delete&lt;/button&gt;';
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;table id="myTable" style="border: 1px solid black"&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;input type="text" id="fname"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow('myTable',0)"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;input type="text" id="fname"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow('myTable',1)"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;input type="text" id="fname"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input type="button" value="Delete" onclick="deleteRow('myTable',2)"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
    
   &lt;p&gt;
  &lt;input type="button" onclick="insRow('myTable')" value="Insert row"&gt;
  &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But i cannot attach the function onclick="deleteRow('myTable',0)" on</p>
  
  <pre><code>z.innerHTML ='&lt;button id="btn" name="btn"&gt; Delete&lt;/button&gt;'
  </code></pre>
  
  <p>¿Is there something else i need to declare in order to make that button work when clicked?</p>
  
  <p>Thanks for your answers</p>