Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Copying Form Fields Into Other Form Fields

Letting a person tap something to copy information from one form field to another is a technique commonly used on purchase order forms, but can be used elsewhere, too.

As an example, the buyer taps something to copy billing information fields into shipping information fields.

Other examples are:

  1. Filling in a home telephone number field from an already-provided work telephone number field.

  2. Copying contact information from the fields for one spouse into fields for the other spouse.

Whenever a field might end up with the same information as another field, the information can be copied from one field to the other. A tap is easier than retyping information.

A Working Example

This forms second set of location information will be copied from the first set when the "(Same as above.)" link is tapped.

Postal Mailing Address

Name:

Address:

City:

State or Province:

Zip or Postal Code:

Country:

Shipping Address

(Same as above.)

Name:

Address:

City:

State or Province:

Zip or Postal Code:

Country:

You see how it works. The shipping location fields are quickly filled with the postal information fields.

Here is the source code of the form fields of the example. Comments follow.

<div style="border-left:3px solid #ccc; padding-left:.75em; border-top-left-radius:1em; border-bottom-left-radius:1em;">
<p style="font-weight:bold;">
Postal Mailing Address
</p>
<p>
Name:<br><input type="text" id="contact-name" name="ContactName">
</p>
<p>
Address:<br><input type="text" id="contact-address" name="ContactAddress">
</p>
<p>
City:<br><input type="text" id="contact-city" name="ContactCity">
</p>
<p>
State or Province:<br><input type="text" id="contact-province" name="ContactProvince">
</p>
<p>
Zip or Postal Code:<br><input type="text" id="contact-postal" name="ContactPostal">
</p>
<p>
Country:<br><input type="text" id="contact-country" name="ContactCountry">
</p>
</div>
<div style="border-left:3px solid #ccc; padding-left:.75em; border-top-left-radius:1em; border-bottom-left-radius:1em;">
<p style="font-weight:bold;">
Shipping Address
</p>
<p>
<a href="javascript:CopyFormFields()">(Same as above.)</a>
</p>
<p>
Name:<br><input type="text" id="shipping-name" name="ShippingName">
</p>
<p>
Address:<br><input type="text" id="shipping-address" name="ShippingAddress">
</p>
<p>
City:<br><input type="text" id="shipping-city" name="ShippingCity">
</p>
<p>
State or Province:<br><input type="text" id="shipping-province" name="ShippingProvince">
</p>
<p>
Zip or Postal Code:<br><input type="text" id="shipping-postal" name="ShippingPostal">
</p>
<p>
Country:<br><input type="text" id="shipping-country" name="ShippingCountry">
</p>
</div>

When the "(Same as above.)" link is tapped the values in the form fields colored blue in the postal section are copied into the form fields colored red in the shipping section.

The link URL is javascript:CopyFormFields(), which calls the JavaScript function that does the copying.

Here is the source code of the entire link:
<a href="javascript:CopyFormFields()">(Same as above.)</a>

Below is the source code of the JavaScript function CopyFormFields() used by the example. Comments follow.

<script type="text/javascript">
function CopyFormFields()
{
    var CopyFromList = new Array( "contact-name", "contact-address", "contact-city", "contact-province", "contact-postal", "contact-country" );
    var CopyToList = new Array( "shipping-name", "shipping-address", "shipping-city", "shipping-province", "shipping-postal", "shipping-country" );
    for( var i=0; i<CopyFromList.length; i++ )
    {
        document.getElementById(CopyToList[i]).value = document.getElementById(CopyFromList[i]).value;
    }
}
</script>

Notice the lists of blue and red id values in the JavaScript. Those are the id value of the forms further above.

The items in the two lists correspond. In other words, the first item in the blue list is "contact-name" and the first item of the red list is "shipping-name". Those two are the id values of corresponding form fields.

Each of the items in the two lists correspond in that way.

The JavaScript can be published anywhere on the page. Immediately above the cancel </body> tag generally is a good place.

When the form goes live, it has a nice convenience for form users, the mark of a professional web site.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC