DOM: appendChild and insertBefore (not insertChild) 

Joined:
02/21/2009
Posts:
49

November 22, 2009 05:50:19
To add a DIV after the last element in body or before the first element:
// append to the end of body
var d1 = document.createElement('DIV');
d1.innerHTML = '<h1>Footer</h1>';
document.body.appendChild(d1);

// insert before the first child
var d2 = document.createElement('DIV');
d2.innerHTML = '<h1>Header</h1>';
document.body.insertBefore(d2, document.body.firstChild);
[ Comment  | Tags ]