help - can anyone tell me how to get this code snippet to work

L

lawrencef

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;
}
}
}
}
}
 
A

apatheticagnostic

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 ifcurrent 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;
                                }
                        }
                }
        }



}


Off the top of my head, try document.mainform.elements[elname].value
 
T

Tom de Neef

<[email protected]> schreef in bericht
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;
}
}
}
}
}

1) Unless mainform is a variabale you have assigned the value "mainform"
before, use
if (document.forms[f].name == "mainform")
2) elname is returned as a string. To use it in accessing a property with
that name, use
document.mainform.elements[elname].value = ...

Tom
 
A

apatheticagnostic

1) Unless mainform is a variabale you have assigned the value "mainform"
before, use
if (document.forms[f].name == "mainform")

mainform is passed into the function
 
T

Tom de Neef

"apatheticagnostic" <[email protected]> schreef in bericht
1) Unless mainform is a variabale you have assigned the value "mainform"
before, use
if (document.forms[f].name == "mainform")

: mainform is passed into the function

Yep!
But then... it is likely passed as a DOM element, not as a string.
Should´nt the conditional then be
if (document.forms[f] == mainform)
rather than
if (document.forms[f].name == mainform)

or
if (document.forms[f].name == mainform.name)

Tom
 
A

apatheticagnostic

"apatheticagnostic" <[email protected]> schreef in bericht
1) Unless mainform is a variabale you have assigned the value "mainform"
before, use
if (document.forms[f].name == "mainform")

: mainform is passed into the function

Yep!
But then... it is likely passed as a DOM element, not as a string.
Should´nt the conditional then be
if (document.forms[f] == mainform)
rather than
if (document.forms[f].name == mainform)

or
if (document.forms[f].name == mainform.name)

Tom

I assumed it was a string because of:

document.forms[f].name == mainform

and

document.mainform.elements.elname.value


I don't have any idea how it actually is, but if it was passed as a
dom element, these two lines would be different (I would assume)

If it is a string, document[mainform].elements[elname].value may do
the trick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top