Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getElementById() has no properties

    This is totally rediculous...
    Can someone please give me a clue as to why the below script does NOTHING?!

    This isn't the actual issue, merely a stripped down test case. document.getElementById() has stopped working for me...

    Code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>getElemTest</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    	document.getElementById('list').style.display = "none";
    	document.getElementById('formFinal').style.border = "1px solid red";
    </script>
    </head>
    
    <body>
    <ul id="list">
    <li>Foo!</li>
    </ul>
    <div id="formFinal">
    <p>Fooo!!!!!!</p>
    </div>
    </body>
    Little help?

    I take no responsibility for the above nonsense.


    Left Justified

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That appears to be the common mistake of trying to access properties which are yet to exist... it should work after the onload event.
    *this message will self destruct in n-seconds*

  • #3
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If only it were that simple...

    Ok, bad example
    In the real scenario, the getElem call is inside a function which is called onload. Here's the snippet in question:
    Code:
    addEvent(window, "load", addAns('woot'), false);
    // ADD ANSWER
    function addAns(ans) 
    {
    /*** Create list item for answer  ***/
    	// create <img> element inside delete link
    	var imgAttr = { 'src' : 'img/del-btn.gif', 'alt' : '', 'height' : '14', 'width' : '14' };
    	var delImg = document.createHTMLElement('img' , imgAttr);
    	// create 'delete link'
    	var aAttr = { 'href' : 'delAnswer.php?id=' };
    	var delLink = document.createHTMLElement('a' , aAttr);
    	//create list item
    	var liAttr = { 'text' : ans };
    	var delItem = document.createHTMLElement('li' , liAttr);
    	// compile created elements
    	delLink.appendChild(delImg);
    	delItem.appendChild(delLink);
    	document.getElementById("anslist").appendChild(delItem);
    /*** Create form item for answer ***/
    	var inputAttr = { 'type' : 'hidden', 'value' : ans };
    	var inputAns = document.createHTMLElement('input' , inputAttr);
    	document.getElementById('formFinal').appendChild(inputAns);
    }
    The addEvent function and document.createHTMLElement prototype are declared elsewhere and work fine -- I'm just attaching the function to window.onload as a means of testing.
    The problem is this line:
    document.getElementById("anslist").appendChild(delItem);
    it always errors - "document.getElementById("anslist") has no properties"
    It also throws the same error on the last line:
    document.getElementById('formFinal').appendChild(inputAns);

    Any more clues? Want the whole thing?

    I take no responsibility for the above nonsense.


    Left Justified

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dunno, if it has no properties, then you're not getting that object for whatever reason... would there happen to be an element w/that id in the document?
    *this message will self destruct in n-seconds*

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    The way you're attaching the event is you're calling it.

    addEvent(window, "load", addAns('woot'), false);

    If the 3rd parameter expects a function object, then this is how you should pass it.

    addEvent(window, "load", function(){addAns('woot')}, false);


    In case there's no parameter, you just pass the function without ().

    addEvent(window, "load", addAns, false);

  • #6
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    GLENN_ROCKS++;

    Cheers mate

    Quote Originally Posted by codegoboom
    would there happen to be an element w/that id in the document?
    What do you think??!
    Last edited by mindlessLemming; 03-14-2005 at 06:01 AM.

    I take no responsibility for the above nonsense.


    Left Justified

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, I was lookin' at the problem of a mindlessLemming, after all...
    *this message will self destruct in n-seconds*

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Quote Originally Posted by mindlessLemming
    GLENN_ROCKS++;

    Cheers mate
    Welcome.
    Actually, it took quite a while before I spotted the cause of the error. I was typing a debugging technique as a reply when I saw it.


  •  

    Posting Permissions

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