SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Access Newly Created DOM Node

    Code JavaScript:
    var div = document.createElement('div');
    var divAp = document.body.appendChild(div);
    //Now if I want to get to it...
    alert(document.body.div); //Nothing there
    alert(document.body.divAP); //Nothing there
    alert(div.parentNode) //BODY
    alert(divAp.parentNode) //BODY

    What is a fail-safe way to access this newly created dom node (besides giving it an ID)?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The first two alerts won't work. And the second two show you that the best way to access these new nodes is simply using your div and divAp (both point to the same thing) variables. div and divAp are both references to the same node. You can do whatever you want with it, just as if you had created the reference using getElementById.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing like DOM hell. Thanks Raffles.


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
  •