Snippets for adding DOM elements dynamically to the defined DOM element.
Here, "mydiv2" is added dynamically as "mydiv"1's child.
//Dynamically generate Div
var newdiv = document.createElement('div');
var divIdName ='mydiv2';
newdiv.setAttribute('id',divIdName);
newdiv.style.width = "565px";
newdiv.style.height = "480px";
newdiv.style.left = "0px";
newdiv.style.top = "0px";
newdiv.style.position = "absolute";
newdiv.style.border = "1px solid #000";
document.getElementById('mydiv1').appendChild(newdiv);
A good example by Dustin Diaz to remove DOM elements Dynamically.
removeElement JavaScript Function
function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
0 comments :
Post a Comment