...

View Full Version : getElementById() has no properties



mindlessLemming
03-14-2005, 05: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:

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? :)

codegoboom
03-14-2005, 05: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.

mindlessLemming
03-14-2005, 06: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);
// 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?

codegoboom
03-14-2005, 06: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

glenngv
03-14-2005, 06: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);

mindlessLemming
03-14-2005, 06:54 AM
GLENN_ROCKS++;

Cheers mate :D



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

What do you think??! ;)

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

glenngv
03-14-2005, 07:27 AM
GLENN_ROCKS++;

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