Creating elements dynamically

Source: Internet
Author: User

To create elements dynamically:
Document.createelement (' element name ');
To delete an element:
Parent. RemoveChild (the element to be deleted);
To add an element:
Parent. appendchild (the element to be added)----> Add to the back;
The parent. InsertBefore (the element to add, the front of whom to add)----> Add to the front of an element;
AppendChild () and insertbefore () are similar to the function of shearing;
Send text:
Window.onload=function () {
var Otxt=document.getelementbyid ("txt");
var Obtn=document.getelementbyid ("btn");
var Obox=document.getelementbyid ("box")

Obtn.onclick=function () {//button click Add New Li;
Sendmess ();
}

Otxt.onkeydown=function (EV) {//otxt, keyboard press to add a new Li;
var oev=ev| | Event
if (oev.keycode==13) {//enter key
Sendmess ();
}
}
function sendmess () {
var ali=document.createelement (' Li '); Create a new element;
Ali.innerhtml=otxt.value+ ' <a href= "javascript:;" > Delete </a> ';
if (oBox.children.length) {//obox.children.length is true, execute if
Obox.insertbefore (Ali,obox.children[0]);
}
Else{obox.appendchild (aLi);}
Otxt.value= ";
Ali.children[0].onclick=function () {//a Click Remove entire LI from UL;
Obox.removechild (This.parentnode);
}
}

}

Creating elements dynamically

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.