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

Discussion in 'Javascript' started by FAQ server, Aug 16, 2010.

  1. FAQ server

    FAQ server Guest

    -----------------------------------------------------------------------
    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.
     
    FAQ server, Aug 16, 2010
    #1
    1. Advertising

  2. On Sun, 15 Aug 2010 23:00:03 +0000, FAQ server wrote:

    > -----------------------------------------------------------------------
    > FAQ Topic - Why does my code fail to access an element?
    > -----------------------------------------------------------------------

    ....
    > Other problems can include::
    > ----------------------------
    >
    > * invalid HTML
    >
    > * two elements with the same `name` or `id`
    >
    > * use of an unsafe name: http://jibbering.com/names/


    404.

    http://jibbering.com/faq/names/ ?


    /Nisse
     
    Nisse Engström, Aug 16, 2010
    #2
    1. Advertising

  3. FAQ server

    amin arab Guest

    usually problem such as this created because position of code is not
    correct in file or maybe have a error before this script that load
    page stoped.

    my offer is

    1- check id element .these should be valid.
    2- change position of your code in to js file or at the end of page
    3- test access to element by other ways such as
    document.getElementsByName() //
    var current = document.getElementsByTagName('div');
    for(var i = 0 ; i < current.length ; i++){
    if(current.id == 'snurgle'){
    alert(current.parentNode);
    }
    }
     
    amin arab, Aug 17, 2010
    #3
  4. In comp.lang.javascript message <>, Mon, 16 Aug
    2010 11:04:26, Nisse Engström <> posted:

    >On Sun, 15 Aug 2010 23:00:03 +0000, FAQ server wrote:
    >
    >> -----------------------------------------------------------------------
    >> FAQ Topic - Why does my code fail to access an element?
    >> -----------------------------------------------------------------------

    >...
    >> Other problems can include::
    >> ----------------------------
    >>
    >> * invalid HTML
    >>
    >> * two elements with the same `name` or `id`
    >>
    >> * use of an unsafe name: http://jibbering.com/names/



    That omits the commonest (IMHO) cause : spelling error.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Aug 17, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,125
    Smokey Grindel
    Dec 2, 2006
  2. Saraswati lakki
    Replies:
    0
    Views:
    1,389
    Saraswati lakki
    Jan 6, 2012
  3. FAQ server
    Replies:
    0
    Views:
    86
    FAQ server
    Jan 23, 2010
  4. FAQ server
    Replies:
    12
    Views:
    168
    Dr J R Stockton
    Apr 14, 2010
  5. FAQ server
    Replies:
    50
    Views:
    415
    Garrett Smith
    May 31, 2010
Loading...

Share This Page