SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementById(iFrame)

    I'm having trouble finding an element by its ID when the element is within an iFrame. Is this one of those "It's not suppose to work that way!" situations?

    And, before you ask, there's a really good reason that I'm using an iframe; I'm putting a message window over a form with select tags and a submit button. An iFrame will cover these elements in IE whereas a div with a solid background won't. No amount of z-index'ing will change it.

    So, how do I get document.getElementById(AjaxTarget) to work when AjaxTarget is a div within an iFrame?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /*
    You need to get a reference to the document that is contained by the contentWindow property of your iframe:

    if your iframe has an id of 'myframe':
    */
    var inDoc= document.getElementById('myframe').contentWindow.document

    To reference a div named myDiv in the document displayed in the window in the iframe:
    var el= inDoc.document.getElementById('myDiv');

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool.

    I'll have a go at it.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Temecula, CA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I try this
    Code:
    var inDoc = document.getElementById('tdFrame').contentWindow.document;
         inDoc.document.getElementById(AjaxTarget).innerHTML = response;
    I get
    Code:
    Error: inDoc.document has no properties
    But when I use
    Code:
    var inDoc = document.getElementById('tdFrame').contentWindow;
         inDoc.document.getElementById(AjaxTarget).innerHTML = response;
    I get
    Code:
    Error: inDoc.document.getElementById(AjaxTarget) has no properties
    I like the second error message better; at least its giving me that inDoc.document is legitimate.

    Can you see where I'm going wrong here?
    PS: document.getElementById(AjaxTarget).innerHTML was cool outside the iFrame; AjaxTarget is legitimate.

  5. #5
    SitePoint Member jtreefrog's Avatar
    Join Date
    Nov 2003
    Location
    Roseville, Mi
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using the 'frames' collection in the parent document:

    if your frame is named 'tdFrame':
    Code:
    frames['tdFrame'].document.getElementById(AjaxTarget)
    HTH
    Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life.


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
  •