Sample code to dynamically add and delete table rows using js

  • 2020-03-30 00:55:41
  • OfStack

As follows:

//Dynamically add rows
function addRow(){
   var table = document.getElementById("tableID");
   var newRow = table.insertRow(); //Create a new line
   var newCell1 = newRow.insertCell(); //Create a new cell
   newCell.innerHTML = "" ;  //The contents of the cell
   newCell.setAttribute("align","center"); //Set the location
//Dynamically delete rows
function deleteRow(){
   var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
   var styles = document.getElementById("tableID");
<table id="testTbl" border=1>
 The product name 
 Product quantity 
 The product is monovalent 
<select name="a">
   <option value=" electronic "> electronic </option>
   <option value=" Electrical appliances "> Electrical appliances </option>
    <input type="text" name="b">
    <input type="text" name="c">
<input type="button" name="Submit2" value=" add " onclick="addRow()">
function addRow(){
//Add the line
var newTr = testTbl.insertRow();
//Add columns
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
var newTd2 = newTr.insertCell();
var newTd3 = newTr.insertCell();
//Sets the column contents and properties
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)">';
function del(o)
var   t=document.getElementById('testTbl');

Related articles: