JS Dynamic Add Delete Row

Source: Internet
Author: User
JS Dynamic Add delete line code, very practical.

Have done a number of projects are used to add JS dynamic deletion of the application of the row, after writing a variety of programs, and finally sorted out the most convenient application of the program, for future use:

Here is the demo full code:




<title>ace test</title>
<script language= "JavaScript" >
var cgetrow=-99999;
function InsertRow () {
var NewRow = Document.all.ace_hidden_table.rows[0].clonenode (true); Clone a row
document.all ("NEWTB"). AppendChild (NewRow); Add a row that you just cloned
}

function GetRow () {
Get row index
Two parentelement respectively are TD and Tr,rowindex are the properties of TR
This.parentElement.parentElement.rowIndex
Cgetrow=window.event.srcelement.parentelement.parentelement.rowindex;

Delrow (Cgetrow);//click checkbox to delete Row directly.
}
function Delrow (iindex) {
Delete a row
if (iindex==-99999) {
Alert ("System prompt: No line number is selected!");
}else{
Newtb.deleterow (Iindex);
iindex==-99999;//will rowindex restore the default value.
}
}
</script>

<body class= "Dialog_body" >
<form method= "POST" onsubmit= "return Dosubmit" >


<table border= "1" width= "900" cellpadding= "0" cellspacing= "0" class= "table" >

<!--hide table, for cloning the line begin-->
<tbody id= "ace_hidden_table" style= "Display:none" >
<tr>
<td><input type= "checkbox" onclick= "GetRow ()"/></td>
&LT;TD width= "16%" >
<select id= "pid" name= "Project" >
<option value= "" ></option>
<option value= "0" >
Person number
</option>
<option value= "1" >
Name
</option>
</select>
</td>
&LT;TD width= "16%" >
<input id= "Result" type= "text" value= "readonly=" true ">
</td>
</tr>
</tbody>
</table>
<!--hide table for cloned line end-->

<table border= "1" width= "900" cellpadding= "0" cellspacing= "0" class= "table" >
<!--the area to insert a new row begin-->
<tbody id= "NEWTB" >
</tbody>
<!--insert a new row in the region end-->
</table>

<div align= "Right" style= "margin:10px;" >
<input type= "button" onclick= "InsertRow ()" value= "Add one line" >
<input type= "button" onclick= "Delrow (Cgetrow);" value= "delete one row" >
</div>

</form>
</body>



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.