[RESOLVED] document.getElemendById() always returns null
Why does the following piece of code *always* return null?
I cannot figure this out...
Code:
<body>
<div>
<script language=javascript>
alert(document.getElementById("p"));
</script>
<div id="p" style="WIDTH: 600px; HEIGHT: 450px"> </div>
</div>
</body>
- ØØ -
Re: document.getElemendById() always returns null
The getElementById function will only return the id's of elements already rendered. Put it underneath and it will work:
HTML Code:
<body>
<div>
<div id="p" style="WIDTH: 600px; HEIGHT: 450px"> </div>
<script type="text/javascript">
alert(document.getElementById("p"));
</script>
</div>
</body>
Re: document.getElemendById() always returns null
Thanks a lot. Not me needing it though. But I rated you anyway...;)
- ØØ -
Re: document.getElemendById() always returns null
Quote:
Originally Posted by visualAd
The getElementById function will only return the id's of elements already rendered. Put it underneath and it will work:
HTML Code:
<body>
<div>
<div id="p" style="WIDTH: 600px; HEIGHT: 450px"> </div>
<script type="text/javascript">
alert(document.getElementById("p"));
</script>
</div>
</body>
ok..
Adam, what if the code of getElementyById is in the head section of the code?
See, I have an instance where I focus the next element (which is rendered above/before the current element) by using getElementById to access it, the problem it seems is that it cannot find that element. :sick:
Does getElementById work for those elements which are rendered next/below the current element?
Somehow, that seems to be the case in IE. :cry:
What's the difference with accessing elements via Name and accessing elements via ID? :confused:
Re: document.getElemendById() always returns null
I'm just gonna make a new thread for this. :)
Re: document.getElemendById() always returns null
nevermind, it was a bug in my code afterall. :D the function getElementById works fine. :D