Javascript and 2 checkboxes - can't get them to check and uncheck

I’m working with a form that has two separate address sections - one for home address, another for work address. There is a checkbox field at the top of each address section with a single entry - “Check if preferred | Yes”. The fields are called HomePref and WorkPref respectively.

The web client user is supposed to check either the Home or Work checkbox to indicate where they would like to receive their paper mail. I need to make sure that only one of the fields is checked - if they try to check both, the other should deselect.

In the onChange event of the HomePref field, I’ve put the following code:

var frm = window.document.forms[0];

var home = frm.HomePref;

var work = frm.WorkPref;

index=0

if (home[index].checked)

{

	work[index].checked = false;

}

In the WorkPref field, I put the following:

var frm = window.document.forms[0];

var home = frm.HomePref;

var work = frm.WorkPref;

index=0

if (work[index].checked)

{

	home[index].checked = false;

}

I can’t this code to work as I had expected. When I check the second box, I get the following error:

“checked is null or not an object”.

Any ideas? I’m stumped. This seems like it should be easy.

Karen

Subject: two problems

  1. use the onclick event not onchange (onchange is for text input fields)

  2. don’t require array for checkbox as each has only one option e.g.

work.checked = true

not

work[index].checked=true

HTH

Tim

Subject: RE: two problems

Many thanks. I have it working now.

I owe you one. I just couldn’t stare at that code any longer.

Karen