PDA

View Full Version : ASP Form Fill-in



fireplug41
09-20-2005, 03:39 AM
I have a form that has 2 sections. Shipping and Billing information. I have a checkbox for same information. I am trying to figure out how to autofill the Billing fields when they fill in the Shipping information.

Twey
09-20-2005, 03:35 PM
I take it the forms are exactly the same? If so, give both forms a name (shipform and billform) and use:

<script type="text/javascript">
function cloneForm() {
var s = document.forms['shipform'].elements,
b = document.forms['billform'].elements;
for(var i=0;i<s.length;i++) {
b[i].value = s[i].value;
}
}
</script>
<input type="checkbox" onclick="if(this.checked) cloneForm(); else document.getElementById('billform').reset();">
Note, however, that if the billing form has a different number of elements to the shipping form, or they're in different places, this will not work.

fireplug41
09-21-2005, 04:15 AM
is there a way to do it if the fields are all in one form?

Twey
09-21-2005, 06:50 AM
Oh, I see: missed that. Right.

<script type="text/javascript">
function cloneForm() {
var s = document.forms['shipform'].elements,
billingFormOffset = 10; // Number of elements in the "billing" part of the form
for(var i=0;i<billingFormOffset;i++) {
s[i + billingFormOffset].value = s[i].value;
}
}
</script>
<input type="checkbox" onclick="if(this.checked) cloneForm(); else document.getElementById('billform').reset();">That should work. I haven't tested it yet.