Forum Moderators: open

Message Too Old, No Replies

something wrong with iteration

         

jackvull

10:34 am on Jun 3, 2008 (gmt 0)

10+ Year Member



Can anyone see anything wrong with this excerpt of code.
When I use alert(workingstructurewithdivid.length);
I get 111 but it only seems to be going through the code once and stopping

else if (document.getElementById("Division").value >= 1) {
for (i = 0; i < workingstructurewithdivid.length; i++) {
var optn = document.createElement("OPTION");
optn.text = workingstructurewithdivid[i].substring(0,5);
optn.value = workingstructurewithdivid[i];
alert(i);
alert ("DIV:" + document.getElementById("Division").value);
alert (optn.value.substring(6,7));
if ((document.getElementById("Division").value == optn.value.substring(6,7))
&& optn.text != workingstructurewithdivid[i-1].substring(0,5)) { //dup check
document.getElementById("BusUnitLevels").options.add(optn);
}
}
}

jackvull

10:43 am on Jun 3, 2008 (gmt 0)

10+ Year Member



Edit: I'm getting an 'undefined' is null or not an object error but it only comes up some of the time so something in the checks must be null and causes it to fall over. Any ideas how to check?

httpwebwitch

6:04 pm on Jun 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if IE encounters an error within a loop, it dies.

It could mean that "document.getElementById("BusUnitLevels")" doesn't exist, or "document.getElementById("Division")" doesn't have a value, or ... anything within the loop that would trigger that undefined is not an object error.

Open the page in Firefox with Firebug installed, and it may give you a more specific line number where the real error is happening.

daveVk

2:51 am on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



workingstructurewithdivid[i-1]

this could be a problem as i=0 ( index = -1 ) on first interation