Beginner JS---Get the contents of the input box and add it to the table

Source: Internet
Author: User

Press the Add button to add the contents of the input box to the table:

This involves the dynamic creation of table cells that have been said to have been skipped

Only the fetch data is added to the table: through getElementById (*). Value gets the values in the input box, which is added to the table by the above code

Complete code: (note section for more complex methods)

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> fill in the table with values </title>
<style>
div{
Border:solid #eee 1px;
width:300px;

}
. box1{
Margin:auto Auto;
}
. box2{
Margin:auto Auto;
height:300px;
margin-top:100px;
}
table{
Border-collapse:collapse; /* When there are two border lines, only one border line is displayed */
Border:solid 1px;
padding:10px;
}
tbody{
Border:solid 1px;
}
tr,th,td{
Border:solid 1px;
padding:10px;
}
</style>
<body>
<div id= "Box1" class= "Box1" >
<table>

<thead>
<tr style= "background:lightgray;height:35px;" >
<th> name </th>
<th> Study No. </th>
No. </th> &LT;TH&GT;QQ
<th> Operations </th>
</tr>
</thead>
<tbody id= "Tbody" >
<tr>
&LT;TD >data</td>
<td>data</td>
<td>data</td>
<td><a href= "#" > Delete </a></th>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
<td><a href= "#" > Delete </a></th>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
<td><a href= "#" > Delete </a></th>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
<td><a href= "#" > Delete </a></th>
</tr>
</tbody>
</table>

</div>
<div id= "Box2" class= "Box2" >
<div>
Name
<input type= "text" id= "name" >
</div>
<div>
Number
<input type= "text" id= "number" >
</div>
<div>
Qq_number:
<input type= "text" id= "QQ" >
</div>
<input type= "button" value= "adding" id= "Add" >

</div>
<script type= "Text/javascript" >


var Add=document.getelementbyid (' Add ');
Add.onclick=function () {
1. Take the value in the input box
var Name=document.getelementbyid (' name '). Value;
var Number=document.getelementbyid (' number '). Value;
var Qq=document.getelementbyid (' QQ '). Value;
var tr=document.createelement (' tr ');
Tr.innerhtml= "<td>" +name+ "</td>" +
"<td>" +number+ "</td>" +
"<td>" +qq+ "</td>" +
"<td><a href= ' javascript:void (0); ' > Delete </a></td> ";

var a=tr.getelementsbytagname ("a") [0];
a.href= ' javascript:void (0) ';
A.onclick=function () {
var r=confirm (' confirm delete ');
if (r) {
Delete
Get click
var Tr=this.parentnode.parentnode;
Tbody.removechild (TR);
}

}
var tr=document.createelement (' tr ');
var nametd=document.createelement (' TD ');
var numbertd=document.createelement (' TD ');
var qqtd=document.createelement (' TD ');
var atd=document.createelement (' TD ');
var a=document.createelement ("a");
a.href= ' javascript:void (0) ';
a.innertext= "Delete";

3. Create a text node
var nametext=document.createtextnode (name);
var numbertext=document.createtextnode (number);
var qqtext=document.createtextnode (QQ);
var atext=document.createtextnode ("delete");


Put the text in the appropriate TD
Nametd.appendchild (Nametext);
Numbertd.appendchild (Numbertext);
Qqtd.appendchild (Qqtext);

Atd.appendchild (a);


Add TD to TR
Tr.appendchild (NAMETD);
Tr.appendchild (NUMBERTD);
Tr.appendchild (QQTD);
Tr.appendchild (ATD);

var Tbody=document.getelementbyid (' tbody ');
Tbody.appendchild (TR);


}



var Tbody=document.getelementbyid (' tbody ');
var links=document.getelementsbytagname (' a ');

links.href= ' javascript:void (0) ';
var len=links.length;
for (Var i=0;i<len;i++) {
var link=links[i];

Link.onclick=function () {
Prompts the user whether to delete
var r=confirm (' confirm delete ');
if (r) {
Delete
Get click
var Tr=this.parentnode.parentnode;
Tbody.removechild (TR);
}
}
}

</script>
</body>

Beginner JS---Get the contents of the input box and add it to the 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.