JavaScript: billing/shipping address copy Jul24 '05

Quite often web forms ask you for two addresses: billing, and shipping.

If the addresses are, in fact, the same (which they are in most cases), it’s aggravating to have to type the same address twice.

Smart web forms have a little check box, which says something like, "Same as billing?" And if you "check" that checkbox, your address is copied over to the shipping section, so you don’t have to type it twice.

This is an extremely important feature for developers to consistently include. Your customer is the number one priority, and if you can save them steps - by all means, do that.

How it’s done

From a developer’s standpoint, how does this "copy/paste" happen?

JavaScript provides a simple solution. Our example below will demonstrate.

Billing section

Below is example HTML code of the billing section of the form.

Shipping section

Below is example HTML code of the shipping section of the form.

JavaScript code

Notice the name attribute of each <input> element. We will be referring to those names in our script. Also, in the shipping section, notice the first <input> element is the checkbox that asks, "Same as Billing Address?" That <input> element has an attribute called onClick, with a value of same_as_billing(), which is a function that will perform the copy/paste.

The function can be placed anywhere your other JavaScript scripts are placed. Or, if you'd rather keep it to a "page level," go ahead and insert this code anywhere in the page:

This function, same_as_billing() simply goes in and uses the values already present in the billing form, and places them into the same fields in the shipping form.

And, remember, this only happens when the user clicks on the checkbox in the shipping form. The onClick is the event that triggers the copy/paste.

Checked, or not?

Now... what if the user initially checks the box, but then decides that later that she wants it unchecked?

We have to make sure that any information already copied to the Shipping section gets erased.

This is already included in the function.

Example

Enter an address in the fields below:

Billing Address

Toggle the checkbox, and watch the information get copied over - or erased.

Shipping Address

More efficient JavaScript

If anyone knows a more efficient way to achieve the same result, please let me know. I know my JavaScript skills aren’t that efficient yet.

Categories: JavaScript , Tutorials

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

I use a similar method .. However the only thing I cannot figure out is how to make the second options menu for shipping_state to have an equal sel ... Read more.

Excellent! I was looking for something just like this for a long time. Works perfectly, thanks for sharing! ... Read more.

Nice and clean! Thanks :) ... Read more.

Your script doesn't work for me! Works on your example, but your example has a different script. You have two forms and give each a different name, ... Read more.

To get the state from the drop down to copy use 'selectedIndex' instead of value in the copy function for state. ... Read more.

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Popular Pages

  1. Fast rounded corners in Photoshop (4153 recent visits)
  2. PHP – passing variables across pages (1559 recent visits)
  3. JavaScript set selected on load (1290 recent visits)
  4. Removing all child nodes from an element (882 recent visits)
  5. iPod songs out of order? (747 recent visits)
  6. Britney - Everytime piano tab (670 recent visits)
  7. Firefox 3 smart address bar: wildcard search (633 recent visits)
  8. MySQL LEFT JOIN syntax (543 recent visits)
  9. Breathe Me - Sia (509 recent visits)
  10. Tumblr: how blogging should be (405 recent visits)

Similar Entries

Stats

21 unique visits since August 2008

Syndicate

Advertisements