JavaScript implementation of dynamic Add table cell element Input,button (appendchild) _javascript skills

Source: Internet
Author: User
Write a small system, you need to dynamically add form elements, according to their own way to write this tutorial!
I think you have seen a similar effect on many websites!
1, first use the Document.createelement method to create an INPUT element!
Copy Code code as follows:

var newinput = document.createelement ("input");

2, set related properties, such as Name,type, etc.
Copy Code code as follows:

Newinput.type=mytype;
Newinput.name= "INPUT1";

3, with the AppendChild method, the element appended to a label content!
Copy Code code as follows:

Temo.appendchild (Newinput);

Javascrip Core Code:
Copy Code code as follows:

<script language= "JavaScript" >
function AddElement (mytype) {
var Mytype,temo=document.getelementbyid ("Add");
var newinput = document.createelement ("input");
Newinput.type=mytype;
Newinput.name= "INPUT1";
Temo.appendchild (Newinput);
var newline= document.createelement ("BR");//Create a BR label to be able to wrap the line!
Temo.appendchild (newline);
}
</script>

<ptml > <pead> <title> Dynamic Add table cell element </title> </pead> <script language= "JavaScript" > Fu Nction addelement (mytype) {var Mytype,temo=document.getelementbyid ("Add"); var newinput = document.createelement ("input"); Newinput.type=mytype; Newinput.name= "INPUT1"; Temo.appendchild (Newinput); var newline= document.createelement ("BR"); Temo.appendchild (newline); } </script> <body> <form action= "" method= "Get" name= "frm" > <div id= "Add" > <i Nput type= "text" name= "TextField" > </div> </form> <input name= "" type= "button" value= "New text box" Oncli ck= "addelement (' text ')"/> <input name= "" type= "button" value= "New checkbox" onclick= "AddElement (' checkbox ')/> &L T;input name= "type=" button "value=" New Radio Box "onclick=" addelement (' Radio ') "/> <input name=" "type=" button "value=" New file Field "onclick=" addelement (' file ') "/> <input name=" "type=" button "value=" New Password Box "onclick=" addelement (' pAssword ') "/> <input name=" "type=" button "value=" New Submit button "onclick=" addelement (' Submit ') "/> <input name= "Type=" button "value=" New Restore button "onclick=" addelement (' reset ') "/> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.