As shown below:
Copy the code code as follows:
//Add rows dynamically
function addRow(){
var table = document.getElementById("tableID");
var newRow = table.insertRow(); //Create a new row
var newCell1 = newRow.insertCell(); //Create a new cell
newCell.innerHTML = ""; //Content within the cell
newCell.setAttribute("align","center"); //Set the position
}
//Dynamicly delete rows
function deleteRow(){
var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
var styles = document.getElementById("tableID");
styles.deleteRow(rowIndex);
}
<html>
<head>
<title></title>
</head>
<body>
<table id="testTbl" border=1>
<tr>
<td>
Product name
</td>
<td>
Product quantity
</td>
<td>
Product unit price
</td>
</tr>
<tr>
<td>
<select name="a">
<option value="Electronic">Electronic</option>
<option value="Electrical appliances">Electrical appliances</option>
</select></td>
<td>
<input type="text" name="b">
</td>
<td>
<input type="text" name="c">
</td>
</td>
</table>
<input type="button" name="Submit2" value="Add" onclick="addRow()">
<script>
function addRow(){
//add row
var newTr = testTbl.insertRow();
//add column
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
var newTd2 = newTr.insertCell();
var newTd3 = newTr.insertCell();
//Set column content and attributes
newTd0.innerText = document.all("a").options[document.all("a").selectedIndex].text;
newTd1.innerText = document.all("b").value;
newTd2.innerText = document.all("c").value;
newTd3.innerHTML= '<input type="button" name="del" value="Delete" onclick="del(this)">';
}
functiondel(o)
{
var t=document.getElementById('testTbl');
t.deleteRow(o.parentNode.parentNode.rowIndex)
}
</script>
</body>
</html>