I'm back again, trying to do some more complicated things with my dynamic forms.
I am trying to add a "Copy Values" button to my form, that will copy a user's physical address into the form fields for the user's billing address.
Unfortunately, my form elements seem to have names that I can't easily identify with javascript. Because I generate a dynamic number of "sub-forms" within my form (they don't have their own "form" tags, but they all have the same form elements), and assign names like "ClientName[0]" for the client name textbox in the first set of form elements, "ClientName[1]" in the second set, etc. (each of the numbers within brackets are defined with an $i variable in my PHP script when it generates the form), I have trouble getting ahold of those elements in javascript.
I have added a button to my "sub-forms" that has an onClick event handler to copy the values. The onClick calls a javascript function, with the $i variable set in the function call. So, if someone pressed the copy button on the second set of form elements, it would call the javascript function like:
Code:
onClick='copy_res(1);'
If someone clicked the copy button on the seventh "sub-form", it would call it like:
Code:
onClick='copy_res(6);'
...and so on and so on.
My javascript function currently looks something like:
Code:
function copy_res(elnum) {
document.frmReport.res_bill_name[elnum].value = document.frmReport.res_name[elnum].value;
document.frmReport.res_bill_addy[elnum].value = document.frmReport.res_addy[elnum].value;
document.frmReport.res_bill_city[elnum].value = document.frmReport.res_city[elnum].value;
document.frmReport.res_bill_state[elnum].value = 46;
}
It doesn't work. The javascript error returned in Firefox's javascript console is:
Quote:
|
document.frmReport.res_bill_name has no properties
|
Obviously, it is hitting that opening bracket and stopping the javascript code with an error.
Can anyone think of a way around this problem without renaming all of my form elements? The PHP code I have written relies very heavily on the fact that each of my form elements encloses a number within those brackets, and it would be a gigantic pain to try to replace them all with something else (and that doesn't even take into account the endless hours I spent trying to figure out a way to generate and evaluate those forms before I eventually figured out the bracket solution).