469,275 Members | 1,773 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Index Counter Problem?

chunk1978
224 100+
hi everyone...

i have a select menu on my form with 3 options:

1. Courier
2. Email
3. Local Pick-Up

i've included an "onChange" event handler on a different select menu to add or remove the 3rd option from this select menu...

there is one main function (Toggle Function) with triggers 2 other functions (either Add or Remove functions)... right now the Remove function works, but i can't get the Add function to work. i don't understand what i'm doing wrong.

this is the code:

Expand|Select|Wrap|Line Numbers
  1. function ToggleLocalPickUp()
  2.     {
  3.     var Province = document.getElementById('provinceterritory').value;
  4.     if (Province=="ProvinceTerritory11") {
  5.     AddLocalPickUp()
  6.     }
  7.     else {
  8.     RemoveLocalPickUp()
  9.     }
  10. }
  11.  
  12. function AddLocalPickUp()
  13.     {
  14.     if (document.Form.receivingoption.options[3].text != "Local Pick-Up") {
  15.     var indexCounter;
  16.     var localpickupchoice = document.Form.receivingoption;
  17.     var lastoption = new Option();
  18.     localpickupchoice.options[3] = lastoption;
  19.  
  20.     for (indexCounter = 3;indexCounter > 3; indexCounter--)
  21.     {
  22.     localpickupchoice.options[indexCounter].text = localpickupchoice.options[indexCounter - 1].text;
  23.     localpickupchoice.options[indexCounter].value = localpickupchoice.options[indexCounter - 1].value;
  24.     }
  25.  
  26.     var option = new Option("Local Pick-Up", "ReceivingOption3");
  27.     localpickupchoice.options[3] = option;
  28.     }
  29. }
  30.  
  31. function RemoveLocalPickUp()
  32.     {
  33.     if (document.Form.receivingoption.options[3].text == "Local Pick-Up") {
  34.     document.Form.receivingoption.options[3] = null;
  35.     }
  36. }
  37.  
i am thinking that maybe there is an issue with the way i have the indexCounter set up? but i really don't know...
Feb 10 '07 #1
10 1038
duye
10
for (indexCounter = 3;indexCounter > 3; indexCounter--)

indexCounter > 3 ; this could never be true, if it is =3, how could it be great than 3?
So whatever in the for loop never get executed.
Feb 11 '07 #2
chunk1978
224 100+
for (indexCounter = 3;indexCounter > 3; indexCounter--)

indexCounter > 3 ; this could never be true, if it is =3, how could it be great than 3?
So whatever in the for loop never get executed.
hi there. yes i realized that, but writing "indexCounter > 2" doesn't work either... and based on the code tutorial i use to execute this function, this exact phrase is said to be the place holder for the new option to be inserted...so i need to add an option in the 3rd position ("indexCounter > 3")... ???

these functions have worked great for me in the past if the select option i am removing and adding is somewhere in the middle of a list... but now i'm having a very difficult time trying to make the last option of the select menu work with these functions... also, i am unable to just place the last option in the middle to make this function work, as it would ruin other functions that have been already written...

so i guess i'm asking how is it possible to write this function to make it work for adding/removing a select option that is the last option in the list?
Feb 11 '07 #3
acoder
16,027 Expert Mod 8TB
Your add code does not make sense.

Are you trying to add or also change the text? What if it has already been added? Are you sure you have three options - options[3] would be the 4th item.
What are you trying to do in the for loop? Are you trying to move all the options up one place?

If you can provide answers, I think your problem is easy to solve.

Perhaps this page might help.
Feb 12 '07 #4
chunk1978
224 100+
Your add code does not make sense.

Are you trying to add or also change the text? What if it has already been added? Are you sure you have three options - options[3] would be the 4th item.
What are you trying to do in the for loop? Are you trying to move all the options up one place?

If you can provide answers, I think your problem is easy to solve.

Perhaps this page might help.
hi acoder...

first, my form loads a select menu ("Receiving Choice") by default with 4 options:

options[0]=(blank space)
options[1]="Courier"
options[2]="Email"
options[3]="Local Pick-Up"

the problem i was having is that this select menu is triggered by other select menu's in the form. for example, if the user select's "United States" as their country of residence, then options[3]="Local Pick-Up"=null. also, if a user selects they are ordering printed images, since i can't email them, options[2]="Email"=null... leaving the user the only remaining option of "Courier".

since a user can always change there mind, i had to write functions that would support these options being rewritten in this select menu. (add)

concerning the "for loop"... well, i have no idea what that is suppose to do... i used a code example to write this...

i managed to write different functions for this problem, to make it work... and it's works fine... but it TOTALLY messed up my other code on the page and no i'm having major issues trying to figure out why other parts of the code (which aren't related to this code) are no longer working...

concerning the issue of the Index Counter, this is what i wrote... maybe you can see something that i can't, that would explain why all my other code no longer works:

Expand|Select|Wrap|Line Numbers
  1. function ToggleEmail() 
  2.     {
  3.      var ChoiceA = document.getElementById("print4x6").value;
  4.      var ChoiceB = document.getElementById("print5x7").value;
  5.     var ChoiceC = document.getElementById("print8x10").value;
  6.     var ChoiceD = document.getElementById("print8x12").value;
  7.     var ChoiceE = document.getElementById("print12x18").value;
  8.     var ChoiceF = document.getElementById("printoriginalsize").value;
  9.     var ChoiceG = document.getElementById("assignmentcheck").value;
  10.     var ChoiceH = document.getElementById("print4x6class2").value;
  11.      var ChoiceI = document.getElementById("print5x7class2").value;
  12.     var ChoiceJ = document.getElementById("print8x10class2").value;
  13.     var ChoiceK = document.getElementById("print8x12class2").value;
  14.     var ChoiceL = document.getElementById("print12x18class2").value;
  15.     var ChoiceM = document.getElementById("printoriginalsizeclass2").value;
  16.     var ChoiceN = document.getElementById("assignmentcheckclass2").value;
  17.     if ((ChoiceA=="4x6_0") && (ChoiceB=="5x7_0") && (ChoiceC=="8x10_0") && (ChoiceD=="8x12_0") && (ChoiceE=="12x18_0") && (ChoiceF=="OriginalSize_0") && (ChoiceG!="AssignmentCheck3") && (ChoiceH=="4x6_0class2") && (ChoiceI=="5x7_0class2") && (ChoiceJ=="8x10_0class2") && (ChoiceK=="8x12_0class2") && (ChoiceL=="12x18_0class2") && (ChoiceM=="OriginalSize_0class2") && (ChoiceN!="AssignmentCheck3class2") && (document.IMAGECtrlForm.provinceterritory.value == "ProvinceTerritory11")) {
  18.     AddEmailToReceiving()
  19.       }
  20.     else if ((ChoiceA=="4x6_0") && (ChoiceB=="5x7_0") && (ChoiceC=="8x10_0") && (ChoiceD=="8x12_0") && (ChoiceE=="12x18_0") && (ChoiceF=="OriginalSize_0") && (ChoiceG!="AssignmentCheck3") && (ChoiceH=="4x6_0class2") && (ChoiceI=="5x7_0class2") && (ChoiceJ=="8x10_0class2") && (ChoiceK=="8x12_0class2") && (ChoiceL=="12x18_0class2") && (ChoiceM=="OriginalSize_0class2") && (ChoiceN!="AssignmentCheck3class2")) {
  21.     AddEmailToReceivingB()
  22.     }
  23.     else{ 
  24.     RemoveEmailFromReceiving() 
  25.     }
  26. }
  27.  
  28. function RemoveEmailFromReceiving() 
  29.     {
  30.     if (document.IMAGECtrlForm.receivingoption.options[2].text == "Email") 
  31.     {document.IMAGECtrlForm.receivingoption.options[2] = null;}
  32.     if (document.IMAGECtrlForm.receivingoption.options[3].text == "Email") 
  33.     {document.IMAGECtrlForm.receivingoption.options[3] = null;} 
  34. }
  35.  
  36. function AddEmailToReceiving() 
  37.     {
  38.     var indexCounter;
  39.     var receivingchoice = document.IMAGECtrlForm.receivingoption;
  40.     var lastoption = new Option();
  41.  
  42.     if (document.IMAGECtrlForm.receivingoption.options[2].text != "Email") {
  43.     receivingchoice.options[3] = lastoption;
  44.     for (indexCounter = 3;indexCounter > 2; indexCounter--)
  45.     {
  46.     receivingchoice.options[indexCounter].text = receivingchoice.options[indexCounter - 1].text;
  47.     receivingchoice.options[indexCounter].value = receivingchoice.options[indexCounter - 1].value;
  48.     }
  49.     var option = new Option("Email", "ReceivingOption2");
  50.     receivingchoice.options[3] = option;
  51.     }
  52. }
  53.  
  54. function AddEmailToReceivingB() 
  55.     {
  56.     var indexCounter;
  57.     var receivingchoice = document.IMAGECtrlForm.receivingoption;
  58.     var lastoption = new Option();
  59.  
  60.     receivingchoice.options[2] = lastoption;
  61.     for (indexCounter = 2;indexCounter > 2; indexCounter--)
  62.     {
  63.     receivingchoice.options[indexCounter].text = receivingchoice.options[indexCounter - 1].text;
  64.     receivingchoice.options[indexCounter].value = receivingchoice.options[indexCounter - 1].value;
  65.     }
  66.     var option = new Option("Email", "ReceivingOption2");
  67.     receivingchoice.options[2] = option;
  68. }
  69.  
  70. function ToggleLocalPickUp()
  71.     {
  72.     var Province = document.getElementById("provinceterritory").value;
  73.     if ((document.IMAGECtrlForm.receivingoption.options[2] == null) && (Province=="ProvinceTerritory11")) {
  74.     AddLocalPickUpB()
  75.     }
  76.     else if (Province=="ProvinceTerritory11") {
  77.     AddLocalPickUp()
  78.     }
  79.     else {
  80.     RemoveLocalPickUp()
  81.     }
  82. }
  83.  
  84. function RemoveLocalPickUp()
  85.     {
  86.     if (document.IMAGECtrlForm.receivingoption.options[2].text == "Local Pick-Up")
  87.     {document.IMAGECtrlForm.receivingoption.options[2] = null;}
  88.     if (document.IMAGECtrlForm.receivingoption.options[3].text == "Local Pick-Up")
  89.     {document.IMAGECtrlForm.receivingoption.options[3] = null;}
  90. }
  91.  
  92. function AddLocalPickUp()
  93.     {
  94.     var indexCounter;
  95.     var localpickupchoice = document.IMAGECtrlForm.receivingoption;
  96.     var lastoption = new Option();
  97.     localpickupchoice.options[3] = lastoption;
  98.  
  99.     if (document.IMAGECtrlForm.receivingoption.options[3].text != "Local Pick-Up") {
  100.     for (indexCounter = 3;indexCounter > 2; indexCounter--)
  101.     {
  102.     localpickupchoice.options[indexCounter].text = localpickupchoice.options[indexCounter - 1].text;
  103.     localpickupchoice.options[indexCounter].value = localpickupchoice.options[indexCounter - 1].value;
  104.     }
  105.     var option = new Option("Local Pick-Up", "ReceivingOption3");
  106.     localpickupchoice.options[3] = option;
  107.     }
  108. }
  109.  
  110. function AddLocalPickUpB()
  111.     {
  112.     var indexCounter;
  113.     var localpickupchoice = document.IMAGECtrlForm.receivingoption;
  114.     var lastoption = new Option();
  115.     localpickupchoice.options[2] = lastoption;
  116.  
  117.     for (indexCounter = 2;indexCounter > 2; indexCounter--)
  118.     {
  119.     localpickupchoice.options[indexCounter].text = localpickupchoice.options[indexCounter - 1].text;
  120.     localpickupchoice.options[indexCounter].value = localpickupchoice.options[indexCounter - 1].value;
  121.     }
  122.     var option = new Option("Local Pick-Up", "ReceivingOption3");
  123.     localpickupchoice.options[2] = option;
  124. }
  125.  
Feb 13 '07 #5
acoder
16,027 Expert Mod 8TB
You only need the following:
Expand|Select|Wrap|Line Numbers
  1. function AddEmailToReceiving() 
  2.     {
  3.     var indexCounter;
  4.     var receivingchoice = document.IMAGECtrlForm.receivingoption;
  5.     if (document.IMAGECtrlForm.receivingoption.options[2].text != "Email") {
  6.     var option = new Option("Email", "ReceivingOption2");
  7.     receivingchoice.options[2] = option;
  8. }
and likewise for the rest.
Feb 13 '07 #6
acoder
16,027 Expert Mod 8TB
One other thing: why do you have so many checks to add the email option? You have redundant code where you have all those conditions + one more for the next if statement. If you must have all those conditions, set a variable with those conditions and use that twice.
Feb 13 '07 #7
chunk1978
224 100+
You only need the following:
Expand|Select|Wrap|Line Numbers
  1. function AddEmailToReceiving() 
  2.     {
  3.     var indexCounter;
  4.     var receivingchoice = document.IMAGECtrlForm.receivingoption;
  5.     if (document.IMAGECtrlForm.receivingoption.options[2].text != "Email") {
  6.     var option = new Option("Email", "ReceivingOption2");
  7.     receivingchoice.options[2] = option;
  8. }
and likewise for the rest.
i tried this and it didn't work (??)
Feb 14 '07 #8
acoder
16,027 Expert Mod 8TB
Ok, to create/insert a new option, you need to use new Option, so try the following:
Expand|Select|Wrap|Line Numbers
  1. function AddEmailToReceiving() 
  2.     {
  3.     var receivingchoice = document.IMAGECtrlForm.receivingoption;
  4.     if (receivingchoice.options[2].text != "Email") {
  5.     receivingchoice.options[3] = new Option(receivingchoice.options[2].text,receivingchoice.options[2].value);
  6.     var option = new Option("Email", "ReceivingOption2");
  7.     receivingchoice.options[2] = option;
  8.     }
  9. }
Change your other functions accordingly.
Feb 14 '07 #9
chunk1978
224 100+
works great now acoder... thanks again!
Feb 14 '07 #10
acoder
16,027 Expert Mod 8TB
You're welcome again... and again!
Feb 15 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Maryam | last post: by
5 posts views Thread by Boo | last post: by
10 posts views Thread by hilz | last post: by
8 posts views Thread by Sam | last post: by
9 posts views Thread by Daniel Vukadinovic | last post: by
reply views Thread by George2 | last post: by
9 posts views Thread by Pygmalion | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.