Simple js issue

Sphexi

Diamond Member
Feb 22, 2005
7,280
0
0
I'm trying to remove option values from a standard form, using some javascript. Basically I don't have access to the source of the form itself, and I'd like to show only the options I want. The first part, trimCountry, works fine. The second part, trimProvince, does not, and I suspect it's because of how I have it setup to look for multiple values. I don't see any errors on the page itself, it just doesn't work. Forgive my formatting :)

<script language="JavaScript">
if(document.getElementById('frmPayment').ordCountry)

TrimCountry(document.getElementById('frmPayment').ordCountry);
if(document.getElementById('frmPayment').shipCountry)

TrimCountry(document.getElementById('frmPayment').shipCountry);

if(document.getElementById('frmPayment').ordProvince)

TrimProvince(document.getElementById('frmPayment').ordProvince);

if(document.getElementById('frmPayment').shipProvince)

TrimProvince(document.getElementById('frmPayment').shipProvince);


function TrimCountry(selObj){
for (i= selObj.options.length -1; i >= 0 ; i--) {
if(selObj.options.value != 'CA')
selObj.options = null;

}
}

function TrimProvince(selObj) {
for (i= selObj.options.length -1; i >= 0 ; i--) {
if(selObj.options.value != 'AB' || selObj.options.value != 'BC' || selObj.options.value != 'MB' || selObj.options.value != 'NB' || selObj.options.value != 'NL' || selObj.options.value != 'NT' || selObj.options.value != 'NU' || selObj.options.value != 'ON' || selObj.options.value != 'PE' || selObj.options.value != 'SK' || selObj.options.value != 'NS' || selObj.options.value != 'QC') selObj.options = null;

}
}
</script>

Any ideas?
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
I'm trying to remove option values from a standard form, using some javascript. Basically I don't have access to the source of the form itself, and I'd like to show only the options I want. The first part, trimCountry, works fine. The second part, trimProvince, does not, and I suspect it's because of how I have it setup to look for multiple values. I don't see any errors on the page itself, it just doesn't work. Forgive my formatting :)

Code:
<script language="JavaScript">
   if(document.getElementById('frmPayment').ordCountry)
      TrimCountry(document.getElementById('frmPayment').ordCountry);

   if(document.getElementById('frmPayment').shipCountry)
      TrimCountry(document.getElementById('frmPayment').shipCountry);
 
   if(document.getElementById('frmPayment').ordProvince)
      TrimProvince(document.getElementById('frmPayment').ordProvince);
           
   if(document.getElementById('frmPayment').shipProvince)
      TrimProvince(document.getElementById('frmPayment').shipProvince);
 
   function TrimCountry(selObj){
      for (i= selObj.options.length -1; i >= 0 ; i--)
      {
         if(selObj.options[i].value != 'CA')
            selObj.options[i] = null;
      }
   }
 
   function TrimProvince(selObj)      {          
      for (i= selObj.options.length -1; i >= 0 ; i--) {
         if(selObj.options[i].value != 'AB' || selObj.options[i].value != 'BC' ||
            selObj.options[i].value != 'MB' || selObj.options[i].value != 'NB' ||
            selObj.options[i].value != 'NL' || selObj.options[i].value != 'NT' ||  
            selObj.options[i].value != 'NU' || selObj.options[i].value != 'ON' || 
            selObj.options[i].value != 'PE' || selObj.options[i].value != 'SK' || 
            selObj.options[i].value != 'NS' || selObj.options[i].value != 'QC')                                                
               selObj.options[i] = null;
      }
   }
</script>
Any ideas?

Formated for the Java gurus. We fought long and hard for code blocks, use them :colbert::p
 

CuriousMike

Diamond Member
Feb 22, 2001
3,044
544
136
I'll suggest simplifying your TrimProvice method to start.

You have a dozen "!= this || !=that" in your conditional statement.

Can you pair it down to just two options and verify that works?

Also, it's a little hard to think in terms of "!= this OR != that OR != it".
I might try changing the logic to say " != this AND != that" changing your this and thats to match what's acceptable.
 

Sphexi

Diamond Member
Feb 22, 2005
7,280
0
0
I'll suggest simplifying your TrimProvice method to start.

You have a dozen "!= this || !=that" in your conditional statement.

Can you pair it down to just two options and verify that works?

Also, it's a little hard to think in terms of "!= this OR != that OR != it".
I might try changing the logic to say " != this AND != that" changing your this and thats to match what's acceptable.

I've since dropped it to a single condition, and originally it was using &&, this was just a test to see what would happen. I have a developer locally looking at it now (it's his code), I'm thinking something changed in the form code that I can't touch, and has since broken everything else.
 

Sphexi

Diamond Member
Feb 22, 2005
7,280
0
0
Issue solved (kinda).

The form is configurable to include shipping fields, or to leave them out. The code I had took care of both billing and shipping fields. It looks like it'd just do nothing if the fields didn't exist, for some reason it was getting stuck on the country/shipping part and not moving forward, no errors either. I just took out the if statements dealing with shipping and it works fine.