Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20888 > unrolled thread

FAQ Topic - Why does my code fail to access an element? (2013-08-08)

Started by"FAQ server" <javascript@dotinternet.be>
First post2013-08-07 23:00 +0000
Last post2013-08-09 19:21 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  FAQ Topic - Why does my code fail to access an element? (2013-08-08) "FAQ server" <javascript@dotinternet.be> - 2013-08-07 23:00 +0000
    Re: FAQ Topic - Why does my code fail to access an element? (2013-08-08) Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-08-09 19:21 +0100

#20888 — FAQ Topic - Why does my code fail to access an element? (2013-08-08)

From"FAQ server" <javascript@dotinternet.be>
Date2013-08-07 23:00 +0000
SubjectFAQ Topic - Why does my code fail to access an element? (2013-08-08)
Message-ID<5202d177$0$299$14726298@news.sunsite.dk>
-----------------------------------------------------------------------
FAQ Topic - Why does my code fail to access an element?
-----------------------------------------------------------------------

An element can only be accessed after it exists in the document.

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">
// Don't forget var.
var snurgleEl = document.getElementById("snurgle");
window.alert(snurgleEl.parentNode);
</script>

Example B:

// In the HEAD.
<script type="text/javascript">
window.onload = function(){
var snurgleEl = document.getElementById("snurgle");
};
</script>

Other problems can include::
----------------------------

* invalid HTML

* two elements with the same `name` or `id`

* use of an unsafe name: http://jibbering.com/names/


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

-- 

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

[toc] | [next] | [standalone]


#20904

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-08-09 19:21 +0100
Message-ID<17sbFxmFNTBSFwuW@invalid.uk.co.demon.merlyn.invalid>
In reply to#20888
In comp.lang.javascript message <5202d177$0$299$14726298@news.sunsite.dk
>, Wed, 7 Aug 2013 23:00:00, FAQ server <javascript@dotinternet.be>
posted:

>-----------------------------------------------------------------------
>FAQ Topic - Why does my code fail to access an element?
>-----------------------------------------------------------------------
>
>An element can only be accessed after it exists in the document.
>

An element can *never* be accessed *after* it exists in the document,
except by a saved reference.

An element can be accessed *before* it exists in the document; but only
after it has been created.

An element of the document can only be accessed by "document methods"
while it is an element of the DOM.

-- 
 (c) John Stockton, nr London, UK.  For Mail, see Home Page.  Turnpike, WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web