Dr J R Stockton wrote:
> In comp.lang.javascript message <h264pa$7te$>, Sat,
> 27 Jun 2009 15:00:06, Garrett Smith <> posted:
>> Dr J R Stockton wrote:
>>> In comp.lang.javascript message <4a440178$0$48238$.
>>> dk>, Thu, 25 Jun 2009 23:00:02, FAQ server <>
>>> posted:
>>>> -----------------------------------------------------------------------
>>>> FAQ Topic - I get an error when trying to access an
>>>> element by getElementById but I know it is in the document.
>>>> What is wrong?
>>>> -----------------------------------------------------------------------
>>> Subject is too long.
>> ACK.
>>
>> Lets move this to a new "Errors" section:
>
> "Reported Errors".
>
>> "X is null or undefined"
>
> The FAQ should not, and should not appear to, express error messages
> literally, since they generally vary from browser to browser, and
> probably more so when non-browsers are included.
>
Probably. Understanding what those error messages mean and what causes
them seems like it would be helpful for beginners.
>
>>> "Why can getElementById fail to find an element?"
>>>
>>>> You cannot access elements that appear in the document after you try
>>>> to read them with a DOM method like ` document.getElementById `.
>>> That can be read to give the intended meaning. But its more
>>> natural
>>> interpretation, to one accustomed to reading good English, is that the
>>> act of attempting doc.gEBI disables subsequent access. Omit.
>> ACK.
>>
>> The wording is totally confusing. Please, can you write something up
>> for that?
>
>
> * An element can only be accessed after it has been created
s/created/parsed
But that answer is not complete either, because it does not consider the
possibility of the element being created via createElement.
Instead:
"An element cannot be accessed before it exists in the DOM"
Is simpler and avoids the earlier confusion.
> * JavaScript is case-sensitive
That's a basic syntax feature of the language. Unlikely to be a problem
here.
> * IDs must be unique; the access may find a duplicate ID
> * The element reference may be incorrectly expressed
I don't know what that means.
[snip examples]
>
> I doubt whether it is necessary to include any of those, or any
> examples; once the problem is understood, the cure should be easy but
> will depend on circumstances.
>
Possibly not. I've shortened the examples.
| 8.8 I get an error when trying to access an element by getElementById
| but I know it is in the document. What is wrong?
|
| An element cannot be accessed before it exists in the DOM.
|
| Either: A) include your script after the HTML element it refers to, or
| B) use the "load" event to trigger your script.
|
| Example A:
|
| <div id="snurgle">here</div>
| <script type="text/javascript">
| document.getElementById("snurgle");
| </script>
|
| Example B:
|
| // In the HEAD.
| <script type="text/javascript">
| window.onload = findElement;
|
| function findElement(){
| var snurgle = document.getElementById("snurgle");
| }
| </script>
Garrett
--
comp.lang.javascript FAQ:
http://jibbering.com/faq/