Can't get out of for loop

Last post 07-26-2007 10:45 AM by XanaLyoko. 17 replies.

Sort Posts:

  • Can't get out of for loop

    07-25-2007, 3:35 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    In my javascript page I have this function:

    function getsubsystems(string)
    {
       /* Completely empties the select of all options.
        */
        document.cq_request.system.options.length = 0;
        
        var Qstring = string
                                
        //Adds default choice
        document.cq_request.system.options[0] = new Option("Choose One", "NULL", true);
                                    
        //Gets the number of child systems from the array.  Needed to populate drop down menu
        var amount = document.getElementsByName(Qstring);
                                    
        //alert(amount);//For debugging only.  Returns amount of options that will go into the second drop down menu
                                    
        if (amount.length == 0)
        {
            document.cq_request.system.options[0] = new Option("Choose One", "NULL", false,false);
            document.cq_request.system.options[1] = new Option("No child systems", "none", false,false);
        }
                                    
        for (i=0; i <= amount.length; i++)//does it until the number of options equal the length of the array that contains the child systems
        {
            var child = amount[i].value;
            //alert(child);//Debugging only.  Returns all values of the child system
            document.cq_request.system.options[1+i] = new Option(child, child, false, false);//Adds child systems to drop down menu
        }
        
        for (iLoop = 0; iLoop < document.cq_request.system.options.length + 1; iLoop++)
            {
                if (document.cq_request.system.options[iLoop].value.ToLower() == Qstring)
                {
                    /* A match is found.  Make this the selected option then populate the second drop down   
                    * and then exit the loop.                                                               
                    */
                    document.cq_request.system.options[iLoop].selected = true;
    
                    checkForNull (document.cq_request.system, 'sys_lbl');
                    break;
                }
            }
    }
    My problem is that I have tried using an alert("WTF"); in my code to test it but it works only up to the first for(i = 0; i <= amount.length; i++) but after the end of that loop the alert wont fire.  Why is this happening??  WHat do I have to do to get it working again?
    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
    Filed under: , ,
  • Re: Can't get out of for loop

    07-25-2007, 4:41 PM
    • Loading...
    • triggered
    • Joined on 05-30-2007, 2:10 PM
    • Posts 682

    you should be using getElementsByTagName instead of getElementsByName 

     

    try alerting amount.length and see what it tells you. I bet thats where your problem is.  

  • Re: Can't get out of for loop

    07-25-2007, 5:17 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    Whats the difference?

     Also, is the syntax the same?

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: Can't get out of for loop

    07-25-2007, 5:22 PM
    • Loading...
    • triggered
    • Joined on 05-30-2007, 2:10 PM
    • Posts 682

    well, getElementsByName is a DOM object used to retreive elements with attribute keys name,

     

    like,  <input type="image" name="imgButton1" />

     

    However, the attribute id has been replacing the name attribute, and in xhtml standard compliance, the name attribute is almost no where to be found. So technically you should be using getElementsByTagName.

    Secondly, if your element doesnt have a name attribute, Im not entirely sure if getElementsByName will even return the object, which is why I asked you to alert the length of that array. Does this make sense? I hope this is the problem, cause thats a lot of code to troubleshoot. =)
     

  • Re: Can't get out of for loop

    07-25-2007, 5:26 PM
    Answer
    • Loading...
    • hazmatz
    • Joined on 07-25-2007, 3:20 PM
    • Salina, KS
    • Posts 19

    JavaScript arrays are zero-based.  So there is no amount[i] where i = amount.length.  Change

    for(i = 0; i <= amount.length; i++)

    to

    for(var i=0; i < amount.length; i++)

     Notice I added var i=... Makes sure that your i variable is initialized and used only here--so you don't get JavaScript cross-speak.

  • Re: Can't get out of for loop

    07-25-2007, 5:33 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    Thank you, you just solved it.  It was not the var but it was the <= sign, i just needed to remove =.  Don't know why though..

     Anyways thanks.

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: Can't get out of for loop

    07-25-2007, 5:42 PM
    • Loading...
    • hazmatz
    • Joined on 07-25-2007, 3:20 PM
    • Salina, KS
    • Posts 19

    Yea, sorry, that was the main point of my post actully.

     If there are 5 things in amount, then amount.length = 5, and since it's zero-based, the indexes range from 0 to 4.  5 is outside the bounds, so the loop never finishes.

  • Re: Can't get out of for loop

    07-25-2007, 5:53 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    Thanks, now I understand it.  I don't think that I will make this error again.

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • One more problem now

    07-25-2007, 6:30 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    Now that I can get out of the for loop I have found another error.  In my code that is supposed to select an option from the drop down menu:

     

        for (iLoop = 0; iLoop < document.cq_request.system.options.length; iLoop++)
            {
                if (document.cq_request.system.options[iLoop].value.ToLower() == Qstring)
                {
                    /* A match is found.  Make this the selected option then populate the second drop down   
                    * and then exit the loop.                                                               
                    */
                    document.cq_request.system.options[iLoop].selected = true;
    
                    checkForNull (document.cq_request.system, 'sys_lbl');
                    break;
                }
            }
    

     (I have edited it to avoid the same problem as before)

    it will not select the option.  In some cases this will only generate a single option but it still goes with the default, choose one, does anyone know why this is happening?

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: One more problem now

    07-25-2007, 6:39 PM
    • Loading...
    • hazmatz
    • Joined on 07-25-2007, 3:20 PM
    • Salina, KS
    • Posts 19

    .selected isn't actually a member of option.  Browsers look for it in HTML to see which one to set as default, but it's not in the DOM.

    Use:

    document.cq_request.system.selectedIndex = iLoop;
  • Re: One more problem now

    07-25-2007, 6:54 PM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    The problem with that is that in my main .aspx page I have the same code and it works.

    I just will not work for selecting the child system.  Even when I changed it to document.cq_request.system.selectedIndex = iLoop;.  In fact, when I changed the other function that selects the first drop down menu it did not work and did not select the option but it works the other way.

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: One more problem now

    07-25-2007, 7:40 PM
    • Loading...
    • hazmatz
    • Joined on 07-25-2007, 3:20 PM
    • Salina, KS
    • Posts 19

    Are you sure it's hitting a match?  Have you added in a alert('found it at " + iLoop);

  • Re: One more problem now

    07-26-2007, 9:50 AM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    Now for some reason it is comming up with this problem:
    Object expected.

    The line that it is having issues with is:

    getsubsystems(Qstring);

    With Qstring being the query string.  It may work if I can get it to recognize the Qstring is the only thing that I need to pass in.  Why would it not be accepting Qstring?

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: One more problem now

    07-26-2007, 10:01 AM
    • Loading...
    • XanaLyoko
    • Joined on 07-03-2007, 12:07 PM
    • Posts 68

    scratch that, it just started working again (damn non real problems).  I have tried what you said at it said that it found it at 0, which means that it is finding it with the first value which is null.

    I tried alerting out document.cq_request.system.options[i].value.ToLower and all it comes up with is undefined.  Why would this happen?

    If history is to change let it change. If the world is to be destroyed so be it. If it is my fate to die ... I must simply laugh.
  • Re: One more problem now

    07-26-2007, 10:23 AM
    Answer
    • Loading...
    • hazmatz
    • Joined on 07-25-2007, 3:20 PM
    • Salina, KS
    • Posts 19

    Woah, didn't see that before.  Does the method ToLower() work for you?  It should be toLowerCase().

Page 1 of 2 (18 items) 1 2 Next >