add table row by javascript onclick

 

<input type=“text” name=“” id=“id”>
<input type=“text” name=“” id=“name”>
<input type=“text” name=“” id=“phone”>
<button type=“button” onclick=add_row()>
ADD ROW
</button>
<table id=“ThisTable” border=“1”>
<tr>
<th>Id</th>
<th>name</th>
<th>phone</th>
</tr>
<tr>
<td>Data1</td>
<td>Data1</td>
<td>Data1</td>
</tr>
<tr>
<td>Data2</td>
<td>Data2</td>
<td>Data2</td>
</tr>
</table>
<script>

function add_row() {

var id = document.getElementById(“id”).value;
var name= document.getElementById(“name”).value;
var phone = document.getElementById(“phone”).value;

// console.log(id + name + phone);

var table = document.getElementById(“ThisTable”);
var row = table.insertRow(-1);

var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = id;
cell2.innerHTML = name;
cell3.innerHTML = phone;
}

</script>

Leave a Comment

Your email address will not be published. Required fields are marked *