Using js to dynamically add and delete table rows example code _ javascript skills

Source: Internet
Author: User
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

Related Article

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.