js-dynamically inserting and deleting rows for table

Source: Internet
Author: User

createtime--2017 April 15 08:09:43author:marydonjs-dynamically insert, delete table rows using the InsertRow () and DeleteRow () Methods of table
<inputID= "UserName"type= "button"value= "Add Line"onclick= "InsertRow ();"/><inputID= "UserName"type= "button"value= "Delete Line 1"onclick= "delrow ();"/><inputID= "UserName"type= "button"value= "Delete Line 3"onclick= "delRow3 ();"/><TableID= "Testtab"Border= "1"style= "Border-collapse:collapse;"width= "99%">    <TR>        <TD>A1</TD>        <TD>A2</TD>        <TD>A3</TD>    </TR></Table>
1. Insert Rows Dynamically
/** * Insert a row after the last TR*/functionInsertRow () {varTable = document.getElementById ("Testtab"); varNewRow = Table.insertrow (-1);//new row (last row)    varNEWCELL1 = Newrow.insertcell (0);//first columnnewcell1.innerhtml = "B1"; varNewCell2 = Newrow.insertcell (1);//Second Cellnewcell2.innerhtml = "B2"; varNewCell3 = Newrow.insertcell (2);//a third cellnewcell3.innerhtml = "B3"; Newrow.insertcell (3). InnerHTML = "<input type= ' button ' value= ' delete line 2 ' onclick= ' DelRow2 (this); ' /> "}
2. Delete Rows
/** *function  delrow (    ) {var table = document.getElementById (" Testtab ");     var lastrowindex = table.rows.length-1; // index value of the last TR     Table.deleterow (lastrowindex);    }
/* * * Delete the specified line * @param {Object} rowIndex         * */ function DelRow2 (inputobj) {    var trobj = inputObj.parentNode.parentNode;     var rowIndex = trobj.rowindex;     var table = Trobj.parentnode;    Table.deleterow (RowIndex);}
/** * There are no parameters can be * @param {Object} obj*/functiondelRow3 (obj) {varTableobj =NULL; varRowIndex =-1; if(obj) {tableobj=Obj.parentNode.parentNode.parentNode; RowIndex=Obj.parentNode.parentNode.rowIndex; } Else{tableobj= document.getElementById ("Testtab"); RowIndex= Tableobj.rows.length-1;        } tableobj.deleterow (RowIndex); }

js-dynamically inserting and deleting rows for table

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.