SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Null reference using getElementById & Objects

    I'm simply trying to create an object named dynGridObject and inside this object I have a variable called this.testFrame. However, it contains a null reference to an object. If I run this exact code without "this." it works but of course the var is then local to the method. Any other variables I define this way but not using getElementById work just fine.

    Any suggestions??

    Code:
    function dynGridObject() {
        this.oXmlDom = new XmlDom();        
            ...
        document.write('<iframe id="grid" scrolling="auto" name="grid"></iframe>');    
        this.testFrame = document.getElementById("grid");        
       // Returns a null reference to an object
    }      
            
    dynGridObject.doLoad() = function () {             
        ...
        // do something with this.testFrame
    }

  2. #2
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.write and innerHTML aren't reliable ways to update the DOM if you want to use DOM methods such as getElementById.

    Do something like:
    Code:
    var container = document.getElementById("somediv");
    var gridFrame = document.createElement("iframe");
    gridFrame.id = "grid";
    container.appendChild(gridFrame);
    to write the iframe.

    The container should be the element in which you want to put the iframe. There may be a way to append it to the current point in the document, but I've never had to do this!

    Try looking on google for more information about the DOM methods.

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool beans! It worked.

    To anyone who has the same problem. Just manually create the object using createElement and append it to some sort of container object that is defined prior to calling your method.

    Many thanks, JonSiddle


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
  •