How to use Dom AppendChild ()

Source: Internet
Author: User

How to use Dom AppendChild ()
The AppendChild () method adds a new child node to the end of the node's child node list.

This method can return this new child node.

AppendChild (newchild) parameter description
Nodes added by newchild

AppendChild definition

AppendChild (newchild:node): Node
Appends a node to the ChildNodes array for the node.
Supported:ie 5.0+, Mozilla 1.0+, Netscape 6.0+, Safari 1.0+, Opera 7.0+
Adding a node to an array of child nodes of the specified node is a bit of a mouthful to read, simply to add the element to the specified node
AppendChild usage
Target.appendchild (newchild)

Newchild inserts the last child node as a child node of target

appendchild Example

var newelement = document. Document.createelement (' label ');
NewElement.Element.setAttribute (' value ', ' Username: ');
var usernametext = document. document.getElementById (' username ');
Usernametext.appendchild (newelement);

The AppendChild method in the DOM can add a label or element to an HTML Web page or XML document, or you can add a child element to an existing label. When you add a child element to an element, you can create a new element, or append other elements of the page to the element. For example, you can add a div for the body, or add an LI for ul.
The AppendChild method must specify a parameter. Otherwise there will be an error, which is the HTML tag you want to add, or the XML node element. Please see the example below for details.

<title>appendchild Method Use Example </title>
<body>
<ul id= "a" >
<li>html</li>
<li>css Tutorials </li>
</ul>
<input type= "button" value= "Add" onclick= "Add_li ()"/>
<script language= "Web Effects" >
function Add_li () {
var ul = document.getElementById ("a");
var li = document.createelement ("Li");
li.innerhtml = "Dom";
Ul.appendchild (LI);
}
</script>
</body>

Look at one. Using DOM dynamic increment input to realize multi-file uploading

<form name= "Form1" method= "Post" action= "enctype=" Multipart/form-data "
     <input type= "file" Name= "pic[]"/>
 <input type= "Submit" Name= "sub" value= "Upload"/>
</ Form>
<a href= "#" onclick= "Addinput ()" > Upload a </a>

<script language= "javascript" type= " Text/javascript
Function Addinput ()/Add input node
{
 
 var input=document.createelement ( ' input ');//Create an input node
 var br=document.createelement (' BR ');//Create a BR node
 input.setattribute (' Type ', ' file '; Set the input node type property to File
 input.setattribute (' name ', ' pic[] ');/set the input node Name property to files[]. The array is passed to the server-side
 document.form1.appendchild (BR);//Add nodes to the Form1 form
 document.form1.appendchild ( input);   
}
</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.