Use JS dynamic Add Delete table row of sample code

Source: Internet
Author: User

  This article is mainly on the use of JS dynamic add Delete table row of sample code is introduced, need friends can come over the reference, I hope to help you.

The     code is as follows://Dynamically add Row function AddRow () {   var table = document.getElementById ("TableID");   & Nbsp;var NewRow = Table.insertrow (); Create a new line    var newCell1 = Newrow.insertcell (); Create new cells    newcell.innerhtml = "";//cell contents    newcell.setattribute ("Align", "center"); Set Location}  //dynamic Delete Row function deleterow () {   var RowIndex = Event.srcElement.parentElement.parentElement.rowIndex;    var styles = document.getElementById ("TableID");    styles.deleterow (RowIndex); }   <html> <head> <title></title> </head> <body> <table id= "Testtbl" border =1> <tr> <td> Product name </td> <td> Product quantity </td> <td> product price </td> </tr> <TR&G T <td> <select name= "A" >    <option value= "electronic" > Electronic </option>    <option value= "Electrical" > Electrical </option> </select></td> <td>     ≪input type= "text" name= "B" >      </td> <td>     <input type= "text" Name= "C" >      </td> </td> </table> <input type= "button" Name= "Submit2" value= "Add" Onclick= "AddRow ()" > <script> function AddRow () {//Add line   var newtr = Testtbl.insertrow ();//Add column var newTd0 = Newtr.insertcell (); var newTd1 = Newtr.insertcell (); var newTd2 = Newtr.insertcell (); var newTd3 = Newtr.insertcell (); Set column content and properties   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= ' <input type= "button" Name= "del" value= "delete" onclick= "del (This)" > "; } function del (o) {var   t=document.getelementbyid (' testtbl '); T.deleterow (O.parentnode.parentnode.rowindex)} </script> </body> </html>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.