SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Location
    Cincinnati, OH, USA
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing a Dynamically Created HTML Element (Sans id and class)

    We've placed a third-party ad on our site that is called via a JavaScript include. This JS dynamically inserts a DIV element at the bottom of the page, and then positions it appropriately using inline CSS.

    Every so often the script bugs out and doesn't apply the correct CSS, rendering the ad inappropriately. I have found exactly what is happening and know exactly how to solve the problem, but am having trouble getting access to the dynamic DIV that is created.

    The problem is that the DIV does not have a consistent ID (it has random numbers in it), and there is no class assigned to it either. Since there are no wildcard solutions in CSS (e.g #my_div_* { ... }), I will need to use JS to gain access to this DIV.

    Does anyone have suggestions on how to do this? The DIV is always the last child of the BODY tag, and is loaded after the page is finished rendering. For example:
    Code:
    <body>
     <div id="someOtherDiv1">...</div>
     <div id="someOtherDiv2">...</div>
     <div id="someOtherDiv3">...</div>
     <div id="DynamicallyCreated_RandomNumber">...</div>
    </body>
    I simply need to add an inline style to that DIV element, but thus far have failed at getting to it.

    Thanks for any help!
    Aaron

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I highly recommend using the javascript prototype framework. It is great for stuff like this.
    If that dynamic div were to be the only empty div in your site you could access the div in a cool way like this:

    PHP Code:
    $$('div:empty'); 
    Heres a solution for grabbing the last div in the body without prototype

    PHP Code:
    document.getElementsByTagName('DIV')[(document.getElementsByTagName('DIV').length)-1


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
  •