Forum Moderators: open

Message Too Old, No Replies

If/Else to set field value from another field

         

ntsmedia

11:53 pm on Nov 17, 2009 (gmt 0)

10+ Year Member



My javascript really is lousy and this "should" be easy ... maybe someone can give me a hand?

I've got a typical selection field of countries and want to set the value of a text field with a shipping cost based upon a match to "USA" (If USA then x.00) or another amount for all others (Else y.yy)

Here's what I've got that is not working:

<script type="text/javascript">
function ctryCalc()
{
if (document.form1.country.value == "USA") {
document.form1.shippingcalc.value = "5.95";
}
else
{
document.form1.shippingcalc.value = "9.95";
}
}
</script>

The form's ID is "form1" and the field with the option list is "country" and the field to get the shipping result is "shippingcalc".

I could also grab the two shipping cost values from hidden fields.

Thanks
John

dreamcatcher

10:31 am on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi John,

Easier to use the DOM.


<script type="text/javascript">
function ctryCalc() {
if (document.getElementById('country').value == "USA") {
document.getElementById('cost').value = "5.95";
} else {
document.getElementById('cost').value = "9.95";
}
}
</script>


<select id="country" name="country" onchange="if(this.value!= 0){ctryCalc()}">
<option value="0">Choose..</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
</select>
<input type="text" name="cost" id="cost" value="" />

Think that should work ok.

dc

ntsmedia

7:08 am on Nov 19, 2009 (gmt 0)

10+ Year Member



Many Thanks!

Works like a charm.

Regards
John