JavaScript dynamically adds form elements to form forms

Source: Internet
Author: User

November 7, 2014 17:10:40

I have written a few similar articles before, and now it seems to be more elementary, to get a high-level simple

Scenario: The background to upload the game, the number is not sure, so use dynamic Add input node method to achieve this effect

The main functions used are:

document.getElementById ();

Objnode.parentnode;

Objnode.clonenode ();

Objnode.removeatrribute ();

Objnode.innerhtml ();

Objnode.appendchild ();

Html:

1 <Divclass= "Well well-sm">2     <Divclass= "Form-group">3         <labelclass= "Form-label">Game:</label>4         <inputtype= "File"name= "jietu[]"class= "Form-input">5         <spanclass= "Form-tip"onclick= "Add_jietu ()"><FontColor= "#428bca">Click Add Game</Font></span>6     </Div>7     <Divclass= "Form-group"ID= "Add_jietu">8         <labelclass= "Form-label">Game:</label>9         <inputtype= "File"name= "jietu[]"class= "Form-input">Ten     </Div> One </Div>

Javascript:

1<script type= "Text/javascript" >2 functionAdd_jietu ()3 {4     varAdd_jietu = document.getElementById (' Add_jietu '));5     varNodefather =Add_jietu.parentnode;6     varNode_clone =Add_jietu.clonenode ();7Content =add_jietu.innerhtml;8Node_clone.removeattribute (' id ');9node_clone.innerhtml =content;Ten Nodefather.appendchild (node_clone); One } A</script>

Attention:

1. JS Line 6th uses the "Clone node" function, there is no HTML in the cloned node, requires the 9th line of code to fill the content

2. Use the Clone function, because the method generates a variable type of "node type" can be used in the appendchild () function to do parameters

3. The variables obtained by the nextSibling and LastChild properties of the node are of type text (see in Chrome's debug window)

JavaScript dynamically adds form elements to form forms

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.