943,537 Members | Top Members by Rank

Ad:
Nov 8th, 2008
0

create and append div to body tag

Expand Post »
i have tried a few tutorials but i cant get this working. I am trying to create a div with the id of overlay and insert it right after the body tag when a function is run. can anyone help? thanks
Reputation Points: 37
Solved Threads: 1
Posting Whiz in Training
cmills83 is offline Offline
249 posts
since Jun 2004
Nov 11th, 2008
0

Re: create and append div to body tag

Google for createElement and insertBefore.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Nov 11th, 2008
0

Re: create and append div to body tag

Here's a simple demo, to get you started!

javascript Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title><!--Sample--></title>
  4. <script language="JavaScript"type="text/javascript">
  5. <!--
  6. function createDiv()
  7. { var _body = document.getElementsByTagName('body') [0];
  8. var _div = document.createElement('div');
  9. var _text = document.createTextNode('Creating Div Element')
  10. _div.appendChild(_text);
  11. _body.appendChild(_div);
  12. }
  13. window.onload = createDiv;
  14. -->
  15. </script>
  16. </head>
  17. <body>
  18. </body>
  19. </html>

If you need something else just let me know. Good day to you...
Last edited by essential; Nov 11th, 2008 at 1:18 pm.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Nov 11th, 2008
0

Re: create and append div to body tag

thanks for the replies i ended up doing this

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function createDivs() {
  2. var div = document.createElement('div');
  3. var container = document.createElement('div');
  4. div.id = 'overlay';
  5. container.id = 'form_container';
  6. if (document.body.firstChild){
  7. document.body.insertBefore(div, document.body.firstChild);
  8. document.body.insertBefore(container, document.body.firstChild);
  9. } else {
  10. document.body.appendChild(div);
  11. document.body.appendChild(container);
  12. }
  13. }
Reputation Points: 37
Solved Threads: 1
Posting Whiz in Training
cmills83 is offline Offline
249 posts
since Jun 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Get all children and their childeren - not innerHTML
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Forcing Flash to Show below Javascript Hover Div





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC