lawrencef
05-08-2008, 02:34 AM
I’m new to JavaScript and am trying to creating a dynamic web form in a number of layers. From what I've read, to submit the entire form I need to have all the fields (that are in all the different layers on the page), present in the main form as hidden fields. I've done this and now and trying to write a function to loop through all the layers on the page and then all the elements within those layers and copy the field value to the hidden field of the same name in the main form on the page. Problem is I don’t know how to concatenate a variable into a code statement in JavaScript (assuming that’s how you term it). ie see the script below – in the last line of the script I'm trying to place 2 variables into the statement ie document.MAINFORM.elements.ELNAME. as it's currently written it causes error because mainform and elname are taken literally and there is no form named mainform and no element named elname so I'm assuming I need to amend this line so that the assigned values for these 2 variables are read instead of the literal values. Can anyone point me in right direction or can advise on correct way to do what I'm trying to do if I'm completely barking up the wrong tree.
function populatemainform(mainform)
{
for (var f = 0; f < document.forms.length; f++) //loop thru all the forms in the document (except main one)
{
if (document.forms[f].name == mainform)
{
continue //skip loop if current form is the main form of the page
}
else
{
for (var e = 0; e < document.forms[f].elements.length; e++) //loop thru all elements in the form.
{
var elname = document.forms[f].elements[e].name // assign current element name to elname variable
document.mainform.elements.elname.value = document.forms[f].elements[e].value;
}
}
}
}
}
function populatemainform(mainform)
{
for (var f = 0; f < document.forms.length; f++) //loop thru all the forms in the document (except main one)
{
if (document.forms[f].name == mainform)
{
continue //skip loop if current form is the main form of the page
}
else
{
for (var e = 0; e < document.forms[f].elements.length; e++) //loop thru all elements in the form.
{
var elname = document.forms[f].elements[e].name // assign current element name to elname variable
document.mainform.elements.elname.value = document.forms[f].elements[e].value;
}
}
}
}
}