UrbanDweller Posted January 25, 2012 Share Posted January 25, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/255724-script-stops-after-for-loop-help/ Share on other sites More sharing options...
nogray Posted January 25, 2012 Share Posted January 25, 2012 In both your for loops, you need to use less than (not less or equal) e.g. for(var i = 0; i < forms.length; i++){.... .... for(var ii = 0; ii < divInputs.length; ii++){... Quote Link to comment https://forums.phpfreaks.com/topic/255724-script-stops-after-for-loop-help/#findComment-1310925 Share on other sites More sharing options...
UrbanDweller Posted January 25, 2012 Author Share Posted January 25, 2012 Oh thanks a lot man that made everything work smoothly, Im trying to figure out exactly why that made such a big difference? Quote Link to comment https://forums.phpfreaks.com/topic/255724-script-stops-after-for-loop-help/#findComment-1310928 Share on other sites More sharing options...
nogray Posted January 25, 2012 Share Posted January 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255724-script-stops-after-for-loop-help/#findComment-1310932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.