Sheritlw
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;
}
}
ASKER
I tried your solution but it didn't help.
Any other ideas?
instead of
I mocked up your code to the attached and was receiving "undefined" for
main.html
arr[i].value
, try using just
[arr[i]
.I mocked up your code to the attached and was receiving "undefined" for
arr[i].value
. Changed it to
arr[i]
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("c tl00_Conte ntPlaceHol der1_wzdSe tup_HiConI nfoDirty") .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.len gth);
you'll notice that no javascript alerts fire at all.
now comment out that line
//alert(arr.length='+arr.l ength);
now your alerts will fire up tot he point of the script bombing
Rather than setting the array to contain references to objects, assign the array *values* of the objects
var hiConInfo = document.getElementById("c
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.len
you'll notice that no javascript alerts fire at all.
now comment out that line
//alert(arr.length='+arr.l
now your alerts will fire up tot he point of the script bombing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the info. I am trying some of your suggestions now.
ASKER
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!
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;
}
}
}
}
}
change: for (var i = 0; i < arr.length; ++i) {
to: for (var i = 0; i < arr.length; i++) {