Js simple table add row and delete row operation example

  • 2020-03-30 02:28:39
  • OfStack

 
<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script> 
//Create an HTML element
function $c(tagname){ 
return document.createElement(tagname); 
} 
//The content to be executed after the document is loaded
$(document).ready(function(){ 
//Binds the click event of the add row button
$("#addrow").bind("click",function(){ 
//In the table
var tab = $("#tab"); 
//Create the tr element
var tr = $c("tr"); 
//Append the tr element to the table
tab.append(tr); 
//Create td elements
var td1=$c("td"); 
//The contents of the td element
td1.innerHTML="insert1"; 
//Appends the td element for the newly appended tr
tr.appendChild(td1); 
var td2=$c("td"); 
td2.innerHTML="insert2"; 
tr.appendChild(td2); 
}); 
//Bind the click event of the delete row button
$("#deleterow").bind("click",function(){ 
//Get the first row of the table
var tab = $("#tab tr:eq(0)"); 
//Delete this trip
tab.remove(); 
}); 
}); 
</script> 
</head> 
<body> 
<table border='1' id="tab"> 
<tr> 
<td>123</td> 
<td>456</td> 
</tr> 
</table> 
</br> 
<button id="addrow"> Add the line </button> 
<button id="deleterow"> Delete rows </button> 
</body> 
</html> 

Related articles: