HTML:
<div id="parentElement"></div>
SCRIPT:
// Get a reference to the element in which we want to insert a new node
var parentElement = document.getElementById('parentElement');
for(var i = 1; i < 5; i++) {
// Get a reference to the first child
var theFirstChild = parentElement.firstChild;
// Create a new element
var newElement = document.createElement("div");
var txtElement = document.createTextNode("Hello " + i);
newElement.appendChild(txtElement);
// Insert the new element before the first child
parentElement.insertBefore(newElement, theFirstChild);
}
No comments:
Post a Comment