Checkbox function execution error ...help

I have a checkbox field called “SameAsBillingAddress” which, once cliked, is supposed to copy the values of some fields to other ones (from billing address fields to shipping adress fields).

When the user clicks on the checkbox an IE error message comes up saying “SameAsBillingAddress.checked is null or is not an object”. When I try same in Firefox the following error message comes up saying :

"form.SameAsBillingAddress has no properties

ShipToBillCustomer(form 42e04642140571508425…)42e04642140571508… (line 39)

onclick(click clientX=0, clientY=0)42e04642140571508… (line 1)

if (form.SameAsMailingAddress.checked) {".

The onClick event of the checkbox executes the following function: “ShipToBillCustomer(this.form);”.

The following coe is found in the JSHeader event of the form:

var CustomerAddressShip = “”;

var CustomerCityShip = “”;

var CustomerStateShip = “”;

var CustomerCountryShip = “”;

var CustomerPostalCodeShip = “”;

function InitSaveVariables(form) {

CustomerAddressShip = form.CustomerAddressShip.value;

CustomerCityShip = form. CustomerCityShip.value;

CustomerStateShip = form.CustomerStateShip.value;

CustomerCountryShip = form.CustomerCountryShip.value;

CustomerPostalCodeShip = form.CustomerPostalCodeShip.value;

}

function ShipToBillCustomer(form) {

if (form.SameAsBillingAddress.checked) {

InitSaveVariables();

form.CustomerAddressShip.value = form. CustomerAddress.value;

form.CustomerCityShip.value = form. CustomerCity.value;

form.CustomerStateShip.value = form. CustomerState.value;

form.CustomerCountryShip.value = form. CustomerCountry.value;

form.CustomerPostalCodeShip.value = form. CustomerPostalCode.value;

}

else {

form.CustomerAddressShip.value = CustomerAddressShip;

form.CustomerCityShip.value = CustomerCityShip;

form.CustomerStateShip.value = CustomerStateShip;

form.CustomerCountryShip.value = CustomerCountryShip;

}

}

I based this code based on sample code found on the JavaScript Source Code website (http://javascript.internet.com/forms/copy-fields.html). Can someone show me how to make this work?

Thanks everyone.

Dan

Subject: Checkbox function execution error …help

Dan,

Your field does not have a form, so trying to pass a form to your function in the FIELD’s onClick event like this:

ShipToBillCustomer(this.form);

will not work because in this context, “this” refers to the field, which, again, does not have a form.

Try this:

ShipToBillCustomer(document.forms[0]);

Or some other method…

Gary

Subject: RE: Checkbox function execution error …help

Form elements have their parent node as a .form property – try alerting this.form.name or this.form.method as a diagnostic. (Using this.form is a common method for passing one of multiple forms on a web page to a function from a field or button event.)

Dan, you might want to pass both the checked property and the form into your function, since it will continue to work as expected if you later decide to make the checkbox a radio with two options (same as above/ship to address below). In the multi-choice case, “this” will refer to the instance being clicked (which will also be the one selected).

Subject: RE: Checkbox function execution error …help

Absolutely correct Stan (as usual) – I was having a unix moment…

Gary

Subject: RE: Checkbox function execution error …help

Hi all,Thanks for the advice. However, I do not know where to make the required change and I am still getting an error message (in FireFox) as follows:

//********************

“form has no properties

ShipToBillCustomer(undefined)42e04642140571508… (line 40)

onclick(click clientX=0, clientY=0)42e04642140571508… (line 1)

if (form.SameAsMailingAddress.checked) {“

//********************

and a different error message (in IE7) as follows:

//********************

Error: ‘SameAsBillingAddress’ is null or not an object

//********************

I made only one change to the code in the JSHeader event of the form and it’s the first line (new line to define “form”). I would like to make this work but I am not very well versed in JS. Stan, how do I pass both the checked property and the form into the function? Why is it that the browser is complaining that the “form” is undefined for the ShipToBillCustomer function when I declared it in the first line?

//****New line *****

var form = document.forms[0];

//*****************

var CustomerAddressShip = “”;

var CustomerCityShip = “”;

var CustomerStateShip = “”;

var CustomerCountryShip = “”;

var CustomerPostalCodeShip = “”;

function InitSaveVariables(form) {

CustomerAddressShip = form.CustomerAddressShip.value;

CustomerCityShip = form. CustomerCityShip.value;

CustomerStateShip = form.CustomerStateShip.value;

CustomerCountryShip = form.CustomerCountryShip.value;

CustomerPostalCodeShip = form.CustomerPostalCodeShip.value;

}

function ShipToBillCustomer(form) {

if (form.SameAsBillingAddress.checked) {

InitSaveVariables();

form.CustomerAddressShip.value = form. CustomerAddress.value;

form.CustomerCityShip.value = form. CustomerCity.value;

form.CustomerStateShip.value = form. CustomerState.value;

form.CustomerCountryShip.value = form. CustomerCountry.value;

form.CustomerPostalCodeShip.value = form. CustomerPostalCode.value;

}

else {

form.CustomerAddressShip.value = CustomerAddressShip;

form.CustomerCityShip.value = CustomerCityShip;

form.CustomerStateShip.value = CustomerStateShip;

form.CustomerCountryShip.value = CustomerCountryShip;

}

}