We help IT Professionals succeed at work.
Get Started

Script to copy values not working

Aleks asked
Last Modified: 2016-02-25
I have the script below, it copies values from one field of the form to another (addresses) when a checkbox is checked, then if unchecked the previous values are restored.

For some reason its simply not working:

var ResCareOf = "";
var ResStr = "";
var ResApt = "";
var ResCity = "";
var ResState = "";
var ResZip = "";
var ResCntry = "";
var ResCntryIndex = 0;

function InitSaveVariables(form) {
ResCareOf = form.ResCareOf.value;
ResStr = form.ResStr.value;
ResApt = form.ResApt.value;
ResCity = form.ResCity.value;
ResState = form.ResState.value;
ResZip = form.ResZip.value;
ResCntryIndex = form.ResCntry.selectedIndex;
ResCntry = form.ResCntry[ResCntryIndex].value;


function MailingtoResidenceAddress(form) {
if (form.copy.checked) {
form.ResCareOf.value = form.MailCareOf.value;
form.ResStr.value = form.MailStr.value;
form.ResApt.value = form.MailApt.value;
form.ResCity.value = form.MailCity.value;
form.ResState.value = form.MailState.value;
form.ResZip.value = form.MailZip.value;
form.ResCntry.selectedIndex = form.MailCntry.selectedIndex;

else {
form.ResCareOf.value = ResCareOf;
form.ResStr.value = ResStr;
form.ResApt.value = ResApt;
form.ResCity.value = ResCity;
form.ResState.value = ResState;
form.ResZip.value = ResZip;      
form.ResCntry.selectedIndex = ResCntry;
//  End -->

The checkbox code is:

<input name="copy" type="checkbox" id="copy" value=""/>

The form name is 'form1'  and the field names are correct in the script.
I am not sure what I may be missing.
Watch Question
Most Valuable Expert 2017
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE