Jump to content

Script stops after for loop? HELP


UrbanDweller

Recommended Posts

Hey the script in the second code box is the whole lost of code that runs after a button click and the for loop below in first code box is where Im currently having trouble.

 

 

 

The loop runs though fine checking the if statement and executing the code but once the loop finishes the script wont continue not even a simple alert. The anouying thing is that if i put a break at the end of the if statment and end the loop that way it will continue through the rest of the script fine, unfortunately i need the loop to finish :(

 

Could someone please help a brother out no google search is helping me.

 

Buggy for loop:

var inputPass = true;
for(var ii = 0; ii <= divInputs.length; ii++){
	if(divInputs[ii].className == "text" || divInputs[ii].className == "error"){
		inputPass = false;
		divInputs[ii].className = "error";
	}	
}
if(inputPass == false){
		alert("pppp");
	return false;
}else{

 

Whole script executed by button onClick:

function nextPage(button, f){
var forms = new Array("form_1","form_2","form_3","form_4");
var formLength = forms.length - 1;
for(var i = 0; i <= forms.length; i++){
	if(document.getElementById(forms[i]).className == "activeForm"){ 
		var div = document.getElementById(forms[i]);
		var divInputs = div.getElementsByTagName("input");
		break;
	}
}

var inputPass = true;
for(var ii = 0; ii <= divInputs.length; ii++){
	if(divInputs[ii].className == "text" || divInputs[ii].className == "error"){
		inputPass = false;
		divInputs[ii].className = "error";
	}	
}
if(inputPass == false){
		alert("pppp");
	return false;
}else{
		var nextForm = i+1;
		if(nextForm < formLength){
			document.getElementById("bckBtn").className = "btnDisplay";
			document.getElementById(forms[i]).className = "disabledForm";
			document.getElementById(forms[nextForm]).className = "activeForm";
			document.getElementById("submitFrm").className = "btnHide";
			return;
		}else if(nextForm == formLength){
			document.getElementById("submitFrm").className = "btnDisplay";
			document.getElementById("nxtBtn").className = "btnHide";
			document.getElementById(forms[i]).className = "disabledForm";
			document.getElementById(forms[nextForm]).className = "activeForm";
			return;
		}
}
}

 

Thanks for your time!

Link to comment
https://forums.phpfreaks.com/topic/255724-script-stops-after-for-loop-help/
Share on other sites

The array indices starts at 0, so if any array has 5 items, the last index will be 4.

e.g.

var arr = ['a', 'b', 'c', 'd', 'e'];
alert(arr.length); // alerts 5 since we have 5 items in the array
alert(arr[0]); // alerts a, first item
alert(arr[4]); // alerts e, last item
alert(arr[5]); // error, that would be the sixth item

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.