Dynamic addition of form elements such as input and button (appendchild) implemented by JavaScript)

Source: Internet
Author: User

When writing a small system, you need to dynamically add form elements and write this tutorial in your own implementation method!
I think you have seen similar results on many websites!
1. Use the document. createelement method to create an input element! CopyCode The Code is as follows: var newinput = Document. createelement ("input ");

2. Set relevant attributes, such as name and type.Copy codeThe Code is as follows: newinput. type = mytype;
Newinput. Name = "input1 ";

3. Use the appendchild method to append an element to a tag!Copy codeThe Code is as follows: temo. appendchild (newinput );

Javascrip core code:Copy codeThe Code is 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 for newline!
Temo. appendchild (newline );
}
</SCRIPT>

>


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.