SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Newcastle, England
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation refer to an object not yet in loaded?

    This ones tricky to explain.

    How do you refer to an object such as a drop down <select> box before the <select> box appears in the code.

    Example:

    var the_select = window.document.form1.jump_menu;
    i want to create a variable of the jump_menu form object in the <head> content but because the page hasn't yet loaded or seen 'form1.jump_menu' then the DOM says it has no properties and therefore I cant assign new ones to it.

    Bascially, Im trying to load properties from an Array also in the <head> tags into the options of a <select> drop down box AS THE PAGE LOADS.

    How can it be done?

    Oh Hell!! Heres the whole code - maybe you can work it out!!!!


    // create the links array
    var my_pages = new Array('Counselling Service', 'Aut', 'CSW', 'SWIRL','realDesign');
    var my_links = new Array('http://www.shef.ac.uk/counselling', 'http://www.shef.ac.uk/aut', 'http://www.shef.ac.uk/ssd/central/welfare.html', 'http://www.shef.ac.uk/ssd/swirl','realdesign.users.btopenworld.com');

    // set option values and text to that of the arrays
    var the_select = window.document.form1.jump_menu;
    if (the_select.options.length != my_pages.length)
    {
    for (loop=0; loop < my_pages.length; loop++)
    {
    var new_option = new Option;
    new_option.text = my_pages[loop];
    new_option.value = my_links[loop];
    the_select.options[loop]= new_option;
    }
    }
    Any ideas???

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you have discovered, you can't refer to objects that have not yet been loaded yet (I only learnt this the other day). The solution is either to put your script in a <script> element within the body BELOW the element you want to refer to (in this case the <select> element) or to put your script in a function in the head of the page and call that function from an onLoad event (either in the body tag or in the select tag itself).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •