Sunday, August 15, 2010

JavaScript To Copy Text From One Field To Another

Often when creating a form on a web page, you need your customers to fill out a field such as a mailing address, as well as a billing address. Instead of having your customers fill out the form twice, you can use JavaScript to copy the form's data from one field to another.
Mailing Address 

Name:  
City:  
 Check this box if Billing Address and Mailing Address are the same.Billing Address 

Name:  
City: 


The JavaScript used to achieve the effect looks like this:


function FillBilling(f) {
  if(f.billingtoo.checked == true) {
    f.billingname.value = f.shippingname.value;
    f.billingcity.value = f.shippingcity.value;
  }
}
To add more fields, just add to the parameters shown above...like this:
f.billingstate.value = f.shippingstate.value;
    f.billingzip.value = f.shippingzip.value;
 The HTML for the form you will use looks like this:


 <b>Mailing Address</b>
<br><br>
<form>
<input type="text" name="shippingcity">
<br>
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)">
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<P>
<b>Billing Address</b>
<br><br>
Name:
<input type="text" name="billingname">
<br>
City:
<input type="text" name="billingcity">
</form>












1 comment: