This article mainly introduces the sample code for dynamically adding and deleting table rows using js. If you need it, you can refer to it and hope to help you as follows:
The Code is as follows:
// Dynamically Add rows
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 in 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 ");
Styles. deleteRow (rowIndex );
}
Product Name
|
Product Quantity
|
Unit Price
|
Electronics Electric Appliance |
|
|
Script
Function addRow (){
// Add rows
Var newTr = testTbl. insertRow ();
// Add a column
Var newTd0 = newTr. insertCell ();
Var newTd1 = newTr. insertCell ();
Var newTd2 = newTr. insertCell ();
Var newTd3 = newTr. insertCell ();
// Set the 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 ='';
}
Function del (o)
{
Var t = document. getElementById ('testtbl ');
T. deleteRow (o. parentNode. parentNode. rowIndex)
}
Script