add dynamic input elements

<!DOCTYPE html>

<html>

<head>

<style>

table, td {

  border: 1px solid black;

}

</style>

</head>

<body>


<p>Click the buttons to create and delete row(s) for the table.</p>


<table id="myTable">

<tbody>

  <tr>

    <td>Row1 cell1</td>

    <td>Row1 cell2</td>

  </tr>

  <tr>

    <td>Row2 cell1</td>

    <td>Row2 cell2</td>

  </tr>

  <tr>

    <td>Row3 cell1</td>

    <td onclick="myFunc();">Row3 cell2</td>

  </tr>

</tbody>

</table>

<br>


<input id="hid" value="0">

<button onclick="myCreateFunction()">Create row</button>

<button onclick="myDeleteFunction()">Delete row</button>


<script>   

function myFunc() { 

   var val =  document.getElementById('hid'); 

   var table = document.getElementById("myTable");

   var row = table.insertRow(1);

   //at the end:

   //var row = table.insertRow(myTable.rows.length + 0);

   document.getElementById('hid').value = + val.value + 1;

  

   var cell1 = row.insertCell(0);

   var cell2 = row.insertCell(1);

   cell1.innerHTML = "<input></input>";

   cell2.innerHTML = "<input></input>"; 

}

</script>


</body>

</html>


Comments