1var newDiv = document.createElement("div");
2document.body.appendChild(newDiv);
1document.body.onload = addElement;
2
3function addElement () {
4 // create a new div element
5 var newDiv = document.createElement("div");
6 // and give it some content
7 var newContent = document.createTextNode("Hi there and greetings!");
8 // add the text node to the newly created div
9 newDiv.appendChild(newContent);
10
11 // add the newly created element and its content into the DOM
12 var currentDiv = document.getElementById("div1");
13 document.body.insertBefore(newDiv, currentDiv);
14}
1document.body.onload = addElement;
2
3function addElement () {
4 // create a new div element
5 const newDiv = document.createElement("div");
6
7 // and give it some content
8 const newContent = document.createTextNode("Hi there and greetings!");
9
10 // add the text node to the newly created div
11 newDiv.appendChild(newContent);
12
13 // add the newly created element and its content into the DOM
14 const currentDiv = document.getElementById("div1");
15 document.body.insertBefore(newDiv, currentDiv);
16}
17