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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.