archangelmihail

Create Element Dinamically

Jun 5th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.73 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>||Working with elements||</title>
  5. </head>
  6.  
  7. <body>
  8. <div id='org_div1'> The text above has been created dynamically.</div>
  9. </body>
  10. <script>
  11. document.body.onload = addElement;
  12. var my_div = null;
  13. var newDiv = null;
  14. function addElement () {
  15.   // create a new div element
  16.   // and give it some content
  17.   var newDiv = document.createElement("div");
  18.   var newContent = document.createTextNode("Hi there and greetings!");
  19.   newDiv.appendChild(newContent); //add the text node to the newly created div.
  20.   // add the newly created element and its content into the DOM
  21.   my_div = document.getElementById("org_div1");
  22.   document.body.insertBefore(newDiv, my_div);
  23. }
  24. </script>
  25. </html>
Advertisement
Add Comment
Please, Sign In to add comment