• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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

0
Sheritlw
Asked:
Sheritlw
  • 4
  • 3
1 Solution
 
nap0leonCommented:
try changing line 27:
change: for (var i = 0; i < arr.length; ++i) {
to: for (var i = 0; i < arr.length; i++) {
0
 
SheritlwAuthor Commented:

I tried your solution but it didn't help.
Any other ideas?
0
 
nap0leonCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nap0leonCommented:
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
0
 
nap0leonCommented:
Firebug is your friend.

Tools - Web Developer - Error Console

error recorded on your
alert(arr[i].value);

Open in new window


"Error: arr[i] is null"

Open in new window

0
 
SheritlwAuthor Commented:

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

0
 
SheritlwAuthor Commented:
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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now