App crashing when I try to instantiate an array

I have some java script in my vb.net application and I’ve been trying to figure out why my web app keeps crashing when i try to instantiate an array (i placed a comment where the app is crashing in the code below). Here’s my code…


function CopyItem(plyrDuplicate,copyPlyrPostback) 
{
    var plyrDuplicate = (typeof plyrDuplicate == "undefined")?'defaultValue':plyrDuplicate;
    var firstListBox = document.getElementById('<%= lstPlayerSelect.ClientID %>');
    var secondListBox = document.getElementById('<%= lstPlayerQueue.ClientID %>');
    var postBack = -1;
    if (copyPlyrPostback == 0){
    postBack = 0;
    }
    //alert("in the function");
    //alert(postBack);
    if (postBack == -1)
     {
        for (var i = 0; i < firstListBox.options.length; i++) 
        { 
            if (firstListBox.options[i].selected) 
            {
                var newOption = document.createElement("option");
                newOption.text = firstListBox.options[i].text;
                newOption.value = firstListBox.options[i].value;
                playerVal = newOption.value;
                CheckDuplicate(playerVal);
                var printRec =  CheckDuplicate(playerVal);
                alert(playerVal);
                alert(CheckDuplicate(playerVal));
                if (printRec != true) 
                {
                 //alert(secondListBox.options[secondListBox.options.length]);
                 //alert(secondListBox.options.length);
                 secondListBox.options[secondListBox.options.length] = newOption;
                }
            }         
        }
        if (document.getElementById('ErrorMsg').style.display = 'inherit') 
        {
            document.getElementById('ErrorMsg').style.display = 'none';
        }
     }
    else
     {
        alert("inside other else block, hurray~!!!");
        hdnSessionVariable = '<%=Session("playerId")%>';
        hdnSessionVariable = hdnSessionVariable.split(",");
        for (var ii = 0; ii < hdnSessionVariable.length; ii++)
            {
            for (var i = 0; i < firstListBox.options.length; i++) 
                { 
                  if (hdnSessionVariable[ii] ==  firstListBox.options[i].value) 
                    {
                     //alert("we have a match!!!");
                        var newOption = document.createElement("option");
                        newOption.text = firstListBox.options[i].text;
                        newOption.value = firstListBox.options[i].value;
                        playerVal = newOption.value;
                        CheckDuplicate(playerVal);
                        var printRec =  CheckDuplicate(playerVal);
                        alert(playerVal);
                        alert(CheckDuplicate(playerVal));
                        alert(newOption.value);
                        if (printRec != true) {
                        secondListBox.options[secondListBox.options.length] = newOption; //app crashes on this line
                    }
                } 
             }       
     }
    Updatelist();
    return false;
}

Hopefully the code above is enough for to figure out the issue. However, here’s a mini recap of what’s going on. A user selected a value from firstListBox and the function above copies it over to secondListBox. The code in the first condition works fine, but the code in the else block doesn’t. Now, here’s the catch/difference between the if and else statement…the Else block is basically doing the same thing as the if statement, except the values that needs to be copied are coming from a session variable (hdnSessionVariable = ‘<%=Session(“playerId”)%>’;). I’ve commented out every line in the else statement and the app works until it hits the secondListBox.options[secondListBox.options.length] = newOption line. Anybody have any idea what’s wrong here?

edit: doh, its a dom object, not a usual array. try this:

secondListBox.appendChild(newOption);

Your post made me recheck my code, and sure enough, I found the issue…I was missing a freaking closing curly bracket!!! Wow, i feel like a jackass.

Just wait until you want to improve your code and run it through jslint.com - with “The Good Parts” enabled. It’s known to make grown men cry :goof: