Dynamically add and delete rows and JavaScript tables based on javascript

Source: Internet
Author: User

Dynamically add and delete rows and JavaScript tables based on javascript

Another dynamic control table effect, using JavaScript to dynamically generate table rows and table columns, as well as dynamically Delete these columns and rows. After running the code, click the corresponding function button, you can perform table operations.

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> <input name = "productNumber" type = "text"> </td> <input name = "quantity" type = "text"> </td> <input name = "weight" type = "text"> </td> </tr> </tbody> </table> <button type = "button" onclick = "addTable (); "style =" margin-left: 750px; "> Add row </button>

2. js

// Add the row function addTable () {var tab = document. getElementById ("viewTabs"); // obtain the table var colsNum = tab. rows. item (0 ). cells. length; // Number of columns in the table // current number of rows in the table 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 the column if (I = 0) {tab. rows [rownum]. cells [I]. innerHTML = '<input name = "productName" type = "text"/>';} else if (I = 1) {tab. rows [row Num]. 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 a row </a>';} // delete a row function delRow (obj) {var Row = obj. parentNode ; Var Row = obj. parentNode; // trwhile (Row. tagName. toLowerCase ()! = "Tr") {Row = Row. parentNode;} Row. parentNode. removeChild (Row); // Delete Row}

The above is a small Editor to share with you the JavaScript Implementation of Dynamic addition and deletion of table rows, I hope to help you.

Articles you may be interested in:
  • Dynamic addition of table rows using javascript
  • Dynamically add and delete table rows using javascript (compatible with IE/FF)
  • Js dynamically adds table rows without refreshing [strongly recommended by the customer's house]
  • JavaScript dynamic addition of table rows using templates and tags
  • How does javascript dynamically load tables and dynamically add table rows?
  • Add and delete table row js implementation code dynamically
  • Native JS and JQuery dynamic addition and deletion of table rows

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.