생활코딩 - JavaScript 수업 - dom example4.html

http://test.opentutorials.org/module/3/24

by egoing

HTML

<div id='org_div1'>
    The text above has been created dynamically.
</div>
<script>
    addElement();
</script>

JavaScript

var my_div = null;
var newDiv = null;

function addElement() {
    // create a new div element
    // and give it some content
    newDiv = document.createElement("div");
    newContent = document.createTextNode("Hi there and greetings!");
    newDiv.appendChild(newContent);
    //add the text node to the newly created div.
    // add the newly created element and it's content into the DOM
    my_div = document.getElementById("org_div1");
    document.body.insertBefore(newDiv, my_div);
}