Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list box to function to setting hidden field value 2

Status
Not open for further replies.

onressy

Programmer
Joined
Mar 7, 2006
Messages
421
Location
CA
this keep freezing on me, any suggestions:

<select name="frmProvince" class="bodytxt" onChange="countryDeter();">
<option value='1'>Alberta</option>
.....
<option value='14'>Alaska</option>
...
</select>


<script>
function countryDeter() {
if (document.registrationFrm.frmLanguage.value < '14') {
document.write("<input type='hidden' name='frmCountry' value='Canada'>");
}else
document.write("<input type='hidden' name='frmCountry' value='United States'>");
}
</script>
 
When you do a document.write within a document already loaded, you REPLACE the current document. So you're trying to overwrite your page. What you want is to put the value into an existing hidden field:
Code:
function countryDeter() {
if (document.registrationFrm.frmLanguage.value < '14') {
  document.registrationFrm.frmCountry.value = 'Canada';
} else {
  document.registrationFrm.frmCountry.value = 'United States';
}
And put the hidden form field in your form with an empty or default value.

You were also missing the open brace after the word else.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
for some reason the value always seems to default to the else condition regardless, i even tried this: (any suggestions)

<script>
function countryDeter() {
var a2t = eval(document.registrationFrm.frmLanguage.value);
if (document.registrationFrm.frmLanguage.value < a2t) {
registrationFrm.frmCountry.value = 'Canada';
} else {
registrationFrm.frmCountry.value = 'United States';
}
}
</script>

<input type="hidden" name="frmCountry" value="">
 
Here's some syntax things I'd fix up for your script:

Code:
<script [!]type="text/javascript"[/!]>
function countryDeter() {
   [i][gray]//eval is not necessary here, and is unnecessarily slow[/gray][/i]
   [s]var a2t = eval(document.registrationFrm.frmLanguage.value);[/s]
   var a2t = [!]parseInt[/!](document.[!]forms[/!]["registrationFrm"].[!]elements[/!]["frmLanguage"].value);

   if ([!]parseInt[/!](document.[!]forms[/!]["registrationFrm"].[!]elements[/!]["frmLanguage"].value) < a2t) {
      document.[!]forms[/!]["registrationFrm"].[!]elements[/!]["frmCountry"].value = 'Canada';
   } else {
      [!]document.forms[/!]["registrationFrm"].[!]elements[/!]["frmCountry"].value = 'United States';
   }
}
</script>

<input type="hidden" name="frmCountry" value="">

Now..... for why the condition is always false:

You are setting the variable a2t to the value in the frmLanguage element. Then, in the if statement directly below, you're checking that same element to see if the value is less that the variable that you just stored. So, the value will never be less than, because it will always be the exact same.

I noticed above that you have a dropdown named frmProvince but it is not referenced anywhere in your new code. Is that element supposed to be in the if conditional statement instead?


-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
If a2t is defined like this
>[tt]var a2t = eval(document.registrationFrm.frmLanguage.value);[/tt]
how on earth would you find ever one case where the conditional would evaluate to true?
>[tt]if (document.registrationFrm.frmLanguage.value < a2t) {[/tt]
I wonder...
 
I think kaht has the correct answer: the select list's name is frmProvince, not frmLanguage. That would make sense, since you would be unlikely to set the country based on the language.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
onressy isn't marked for notification so I think this is going to be one of those threads that falls under the radar.....

oh well...

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
I guess since the OP isn't going to come back to credit your with solving the problem, I'll do it.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top