473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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;
}
}
}
}
}
Jun 27 '08 #1
5 1085
On May 7, 9:28*pm, lawren...@hlac.com.au wrote:
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
Jun 27 '08 #2
<la*******@hlac.com.auschreef in bericht
news:3a**********************************@u36g2000 prf.googlegroups.com...
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

Jun 27 '08 #3
On May 8, 3:10*am, "Tom de Neef" <tden...@qolor.nlwrote:
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
Jun 27 '08 #4
"apatheticagnostic" <ap***************@gmail.comschreef in bericht
news:6e**********************************@59g2000h sb.googlegroups.com...
On May 8, 3:10 am, "Tom de Neef" <tden...@qolor.nlwrote:
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
Jun 27 '08 #5
On May 8, 6:24*am, "Tom de Neef" <tden...@qolor.nlwrote:
"apatheticagnostic" <apatheticagnos...@gmail.comschreef in berichtnews:6e**********************************@5 9g2000hsb.googlegroups.com...
On May 8, 3:10 am, "Tom de Neef" <tden...@qolor.nlwrote:
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.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: keyser_Soze | last post by:
I have MS Visual Studio 2003 on Windows XP Pro. I have IIS running on this machine and I am trying to debug some existing code which has both ASP and ASP.NET components. When I try and launch...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
6
by: pamela fluente | last post by:
Hi, please find below a very simple code snippet which is giving me the following error: System.Runtime.Serialization.SerializationException was unhandled Message="The constructor to deserialize...
0
by: mortovski | last post by:
Hi there, I'm having trouble using the C# regex function and any help would be appreciated! Here's a snippet of the code I'm using: matches =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.