By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,398 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,398 IT Pros & Developers. It's quick & easy.

document.createelement('div') doesn't work?

P: 3
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>||Working with elements||</title>
  4. </head>
  5.  
  6. <script type="text/javascript">
  7. var my_div = null;
  8. var newDiv = null;
  9.  
  10. function addElement()
  11. {
  12.   // create a new div element
  13.   // and give it some content
  14.   newDiv = document.createElement("div");
  15.   newDiv.innerHTML = "<h1>Hi there and greetings!</h1>";
  16.  
  17.   // add the newly created element and it's content into the DOM
  18.   my_div = document.getElementById("org_div1");
  19.   document.body.insertBefore(newDiv, my_div);
  20. }
  21.  
  22. </script>
  23.  
  24. <body onload="addElement()">
  25. <div id='org_div1'> The text above has been created dynamically.</div>
  26. <script>
  27. var div = document.getElementsByTagName("div");
  28. document.write("<br>div= "+div.length);
  29. </script>
  30. </body>
  31. </html>
here div.length=1
Jul 29 '11 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,430
It works fine. If you're opening it from your local machine, your browser might be blocking the script.
Jul 29 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.