Example of adding and deleting rows in a simple table in js

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Html>
<Head>
<Script src = "http://code.jquery.com/jquery-1.11.0.min.js"> </script>
<Script>
// Create an html Element
Function $ c (tagname ){
Return document. createElement (tagname );
}
// Content to be executed after the file is loaded
$ (Document). ready (function (){
// Bind the Click Event of the add row button
$ ("# Addrow"). bind ("click", function (){
// Obtain the table
Var tab = $ ("# tab ");
// Create a tr Element
Var tr = $ c ("tr ");
// Append the tr element to the table
Tab. append (tr );
// Create a td Element
Var td1 = $ c ("td ");
// Content of the td Element
Td1.innerHTML = "insert1 ";
// Append the td element to the newly appended tr
Tr. appendChild (td1 );
Var td2 = $ c ("td ");
Td2.innerHTML = "insert2 ";
Tr. appendChild (td2 );
});
// Bind the Click Event of the Delete row button
$ ("# Deleterow"). bind ("click", function (){
// Obtain the first row of the table
Var tab = $ ("# tab tr: eq (0 )");
// Delete this row
Tab. remove ();
});
});
</Script>
</Head>
<Body>
<Table border = '1' id = "tab">
<Tr>
<Td> 123 </td>
<Td> 456 </td>
</Tr>
</Table>
</Br>
<Button id = "addrow"> Add row </button>
<Button id = "deleterow"> delete a row </button>
</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.