View Full Version : getElementById() has no properties

03-14-2005, 04:35 AM
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... :confused:


<!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">
<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";

<ul id="list">
<div id="formFinal">

Little help? :)

03-14-2005, 04:39 AM
That appears to be the common mistake of trying to access properties which are yet to exist... it should work after the onload event.

03-14-2005, 05:06 AM
Ok, bad example :o
In the real scenario, the getElem call is inside a function which is called onload. Here's the snippet in question:

addEvent(window, "load", addAns('woot'), false);
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
/*** Create form item for answer ***/
var inputAttr = { 'type' : 'hidden', 'value' : ans };
var inputAns = document.createHTMLElement('input' , inputAttr);

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:
it always errors - "document.getElementById("anslist") has no properties"
It also throws the same error on the last line:

Any more clues? Want the whole thing?

03-14-2005, 05:15 AM
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? :D

03-14-2005, 05:27 AM
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);

03-14-2005, 05:54 AM

Cheers mate :D

would there happen to be an element w/that id in the document?

What do you think??! ;)

03-14-2005, 06:16 AM
Hey, I was lookin' at the problem of a mindlessLemming, after all... :p

03-14-2005, 06:27 AM

Cheers mate :D
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. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum