Insert or delete a table row in Javascript

Source: Internet
Author: User

<HTML>
<Head>
<SCRIPT type = "text/JavaScript">
Function deleterow (r)
{
VaR I = R. parentnode. parentnode. rowindex
Document. getelementbyid ('mytable'). deleterow (I)
}

Function insertrowintable (){

<HTML>
<Head>
<SCRIPT type = "text/JavaScript">
Function deleterow (r)
{
VaR I = R. parentnode. parentnode. rowindex
Document. getelementbyid ('mytable'). deleterow (I)
}

Function insertrowintable (){
VaR table = Document. getelementbyid ("mytable ");
VaR newrow = table. insertrow (-1 );
Newrow. insertcell (). innerhtml = newrow. rowindex;
Newrow. insertcell (-1). innerhtml = "<input type = 'button 'value = 'delete 'onclick = 'deleterow (this)'> ";
}
</SCRIPT>
</Head>
<Body>

<Table id = "mytable" border = "1">
<Tr>
<TD> Row 1 </TD>
<TD> <input type = "button" value = "delete" onclick = "deleterow (this)"> </TD>
</Tr>
<Tr>
<TD> Row 2 </TD>
<TD> <input type = "button" value = "delete" onclick = "deleterow (this)"> </TD>
</Tr>
<Tr>
<TD> Row 3 </TD>
<TD> <input type = "button" value = "delete" onclick = "deleterow (this)"> </TD>
</Tr>
</Table>
<Input type = "button" onclick = "insertrowintable ();" value = "insert a row"/>
</Body>
</Html>


Deleterow () deletes a row from the table.

Insertrow () inserts a new row into the table.

Innerhtml sets or returns the HTML between the start tag and end tag of the row.

Rowindex returns the position of the row in the table.

VaR table = Document. getelementbyid ("mytable ");
VaR newrow = table. insertrow (-1 );
Newrow. insertcell (). innerhtml = newrow. rowindex;
Newrow. insertcell (-1). innerhtml = "<input type = 'button 'value = 'delete 'onclick = 'deleterow (this)'> ";
}
</SCRIPT>
</Head>
<Body>

<Table id = "mytable" border = "1">
<Tr>
<TD> Row 1 </TD>
<TD> <input type = "button" value = "delete" onclick = "deleterow (this)"> </TD>
</Tr>
<Tr>
<TD> Row 2 </TD>
<TD> <input type = "button" value = "delete" onclick = "deleterow (this)"> </TD>
</Tr>
<Tr>
<TD> Row 3 </TD>
<TD> <input type = "button" value = "delete" onclick = "deleterow (this)"> </TD>
</Tr>
</Table>
<Input type = "button" onclick = "insertrowintable ();" value = "insert a row"/>
</Body>
</Html>

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.