Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

help with javascript function that stops working


I am just learning javascript, so I place alerts to see where a function may stop working.
Everything works as expected up until the for (var i = 0; ....  The alert directly underneath doesn't fire.  I imagine it could be my use of { }.  I haven't quite got that figured out.
The function is called during a wizards next button click event.

FYI...
The purpose of this function is to check if a wizard step has been edited and if "true" then do a click on a button to save the information, otherwise just exit.
I have hidden fields within each step that track whether data has been edited and a primary hidden field, not within steps that I check when unloading the form to see if things need to be saved.

What am I missing in the javascript function below?

Thanks



Private Sub wzdSetup_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wzdSetup.NextButtonClick
      Dim s As String = e.CurrentStepIndex.ToString
      ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "CheckNext", "CheckNext('" & s & "');", True)
   End Sub


function CheckNext(stepnumb) {
     
      var hipage = document.getElementById("ctl00_ContentPlaceHolder1_HiPageDirty");
     
      if (hipage.value == "true") {
        
         alert("hipage.value");
         var hiConInfo = document.getElementById("ctl00_ContentPlaceHolder1_wzdSetup_HiConInfoDirty");
         var hiConCon = document.getElementById("ctl00_ContentPlaceHolder1_wzdSetup_HiConConDirty");
         var hiBio = document.getElementById("ctl00_ContentPlaceHolder1_wzdSetup_HiBioDirty");
         var hiBioCon = document.getElementById("ctl00_ContentPlaceHolder1_wzdSetup_HiBioContDirty");
         var hiDef = document.getElementById("ctl00_ContentPlaceHolder1_wzdSetup_HiDefDirty");
         var arr = new Array(5);
         arr[0] = hiConInfo;
         arr[1] = hiConCon;
         arr[2] = hiBio;
         arr[3] = hiBioCon;
         arr[4] = hiDef;
         alert("array created");
         alert(stepnumb);
         for (var i = 0; i < arr.length; ++i) {
            alert(arr[i].value);
            if (i == stepnumb) {
               var hidirty = arr[i].value;
               alert(hidirty);
               if (hidirty == "true") {
                  var r = confirm("Would you like to save your changes?");
               } 
                  if (r == true){
                     switch (i) {
                        case "0":
                           alert(i);
                        case "1":
                           alert(i);
                           break;
                        case "2":
                           alert(i);
                           break;
                        case "3":
                           break;
                        case "4":
                           
                           break;
                        case "5":
                           break;
                        default:
                           break;
                     }
                  
                  }
               }
               else {
                  
               }

            }
            
            return false;
         }
         
         
         
      }

Open in new window

Avatar of nap0leon
nap0leon

try changing line 27:
change: for (var i = 0; i < arr.length; ++i) {
to: for (var i = 0; i < arr.length; i++) {
Avatar of Sheritlw

ASKER


I tried your solution but it didn't help.
Any other ideas?
instead of
arr[i].value

Open in new window

, try using just
 [arr[i]

Open in new window

.

I mocked up your code to the attached and was receiving "undefined" for
arr[i].value

Open in new window

.  Changed it to
arr[i] 

Open in new window

and it at least alerted what I was expecting.
main.html
Upon closer examination...
Rather than setting the array to contain references to objects, assign the array *values* of the objects
var hiConInfo = document.getElementById("ctl00_ContentPlaceHolder1_wzdSetup_HiConInfoDirty").value;

then make the change I listed above (removing " .value " from your array value tests)

Debug code:
add this alert above your "for (var 1 = 0;..." line:
alert(arr.length='+arr.length);
you'll notice that no javascript alerts fire at all.
now comment out that line
//alert(arr.length='+arr.length);
now your alerts will fire up tot he point of the script bombing
ASKER CERTIFIED SOLUTION
Avatar of nap0leon
nap0leon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Thanks for the info.  I am trying some of your suggestions now.

Thanks so much for your help.
Because the hidden fields were in each step and the NextButtonClick event of the wizard moved to the next screen before the javascript ran, the hidden field value was cleared.
I moved all hidden fields out of the wizard and now it works as expected.
I posted what I've come up with below.

Again, thanks for helping me figure this out!
function CheckNext(stepnumb) {
      var hipage = document.getElementById("ctl00_ContentPlaceHolder1_HiPageDirty");
      if (hipage.value == "true") {
         alert("begin array");
         var arr = new Array(5);
         arr[0] = document.getElementById("ctl00_ContentPlaceHolder1_HiConInfoDirty");
         arr[1] = document.getElementById("ctl00_ContentPlaceHolder1_HiConConDirty");
         arr[2] = document.getElementById("ctl00_ContentPlaceHolder1_HiBioDirty");
         arr[3] = document.getElementById("ctl00_ContentPlaceHolder1_HiBioContDirty");
         arr[4] = document.getElementById("ctl00_ContentPlaceHolder1_HiDefDirty");
         alert("array created");
        
         for (var i = 0; i < arr.length; i++) {
            var pnumb = parseInt(stepnumb);
           
            if (i == pnumb) {
               var hidirty = arr[i].value;
               
               if (hidirty == "true") {
                  var r = confirm("Would you like to save your changes?");

                  if (r == true) {
                     switch (pnumb) {
                        case 0:
                           alert("case 0");
                           break;
                        case 1:
                           alert("case 1");
                           break;
                        case 2:
                           alert("case 2");
                           break;
                        case 3:
                           alert("case 3");
                           break;
                        case 4:
                           alert("case 4");
                           break;
                        
                        default:
                           break;
                     }

                  }
                  else {
                     break;}
               }
               else {
                  break;
               } 
            }
         }
      }
   }

Open in new window