_javascript technique of dynamic adding delete table based on JavaScript implementation

Source: Internet
Author: User
Tags button type

Another dynamic control of the table, with JavaScript dynamic table rows, table columns, and can also dynamically delete these rows, lines, and so on, run the code, click the corresponding function button, you can achieve the corresponding table operation function.

1.jsp

<table id= "Viewtabs" >
<thead>
<tr>
<th> product name </th>
<th> No. < /th>
<th> Quantity </th>
<th> weight </th>
<th> operation </th>
</tr>
</thead>
<tbody>
<tr>
<td><input name= "ProductName" type= "text" ></td>
<td><input name= "ProductNumber" type= "text" ></td>
<td>< Input name= "Quantity" type= "text" ></td>
<td><input name= "Weight" type= "text" ></td>
<td></td>
</tr>
</tbody>
</table>
<button type= " Button "onclick=" addtable (); "style=" margin-left:750px; " > Add Rows </button>

2.js

Add Row function addtable () {var Tab=document.getelementbyid ("Viewtabs");//Get table var colsnum=tab.rows.item (0). Cells.length;
Number of columns in table//table current number of rows Var Num=document.getelementbyid ("Viewtabs"). Rows.length;
var rownum=num;
Tab.insertrow (rownum); for (var i=0;i<4; i++) {Tab.rows[rownum].insertcell (i);//Insert Column if (i==0) {tab.rows[rownum].cells[i].innerhtml= ' <
Input name= "ProductName" type= "text"/> '; }else if (i==1) {tab.rows[rownum].cells[i].innerhtml= ' <input name= ' productnumber ' type= ' text '/> ';} else if (i==2) {tab.rows[rownum].cells[i].innerhtml= ' <input name= ' quantity ' type= ' text '/> ';}
else{tab.rows[rownum].cells[i].innerhtml= ' <input name= ' weight ' type= ' text '/> ';}
Tab.rows[rownum].insertcell (i);
Tab.rows[rownum].cells[i].innerhtml= ' <a href= "#" onclick= "Delrow (This)" > Delete line </a> "; //Delete Row function delrow (obj) {var row=obj.parentnode; var row=obj.parentnode//tr while Row.tagName.toLowerCase ()!= "tr
") {Row=row.parentnode;} Row.parentNode.removeChild (Row); DeleteLine 

The above is a small set to share the JavaScript implementation dynamic add Delete table row, I hope to help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.