SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Create div after body

    Trying to create a div right after the body, but this code doesn't seem to work. Anyone know why?

    Code JavaScript:
    window.onload=function(){
    var bodyTag = document.getElementsByTagName("body")[0];
    var obamaDiv = document.createElement('div');
    obamaDiv.setAttribute('id','obama2008ribbon');
    bodyTag.appendChild(obamaDiv);
    }

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not creating it AFTER the body, you're creating it inside the body as the last element:
    Code:
    <body>
    ...
    <div id="obama2008ribbon"></div>
    </body>
    And it is working. This proves it:
    Code:
    window.onload=function(){
    var bodyTag = document.getElementsByTagName("body")[0];
    var obamaDiv = document.createElement('div');
    obamaDiv.setAttribute('id','obama2008ribbon');
    obamaDiv.appendChild(document.createTextNode("Hello"));
    bodyTag.appendChild(obamaDiv);
    }
    I suspect, though, that perhaps you're having a problem targeting that id. Try this instead:
    Code:
    window.onload=function(){
    var bodyTag = document.getElementsByTagName("body")[0];
    var obamaDiv = document.createElement('div');
    obamaDiv.id = 'obama2008ribbon';
    obamaDiv.appendChild(document.createTextNode("Hello"));
    bodyTag.appendChild(obamaDiv);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •