473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.MAINFO RM.elements.ELN AME.
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 populatemainfor m(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.lengt h; e++) //loop
thru all elements in the form.
{
var elname = document.forms[f].elements[e].name // assign current
element name to elname variable
document.mainfo rm.elements.eln ame.value =
document.forms[f].elements[e].value;
}
}
}
}
}
Jun 27 '08 #1
5 1101
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.MAINFO RM.elements.ELN AME.
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 populatemainfor m(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.lengt h; e++) //loop
thru all elements in the form.
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * * var elname= document.forms[f].elements[e].name // assign current
element name to elname variable
* * * * * * * * * * * * * * * * document.mainfo rm.elements.eln ame.value =
document.forms[f].elements[e].value;
* * * * * * * * * * * * * * * * }
* * * * * * * * * * * * }
* * * * * * * * }
* * * * }

}

Off the top of my head, try document.mainfo rm.elements[elname].value
Jun 27 '08 #2
<la*******@hlac .com.auschreef in bericht
news:3a******** *************** ***********@u36 g2000prf.google groups.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.MAINFO RM.elements.ELN AME.
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 populatemainfor m(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.lengt h; e++) //loop
thru all elements in the form.
{
var elname = document.forms[f].elements[e].name // assign current
element name to elname variable
document.mainfo rm.elements.eln ame.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.mainfo rm.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
"apatheticagnos tic" <ap************ ***@gmail.comsc hreef in bericht
news:6e******** *************** ***********@59g 2000hsb.googleg roups.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:
"apatheticagnos tic" <apatheticagnos ...@gmail.comsc hreef in berichtnews:6e* *************** *************** ***@59g2000hsb. googlegroups.co m...
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.mainfo rm.elements.eln ame.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
3206
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 the debugger from VS, I am told it can't because the project is of output type class library. The error indicates I should set the start action to start external program or start URL. I tried both of these and cannot seem to get the debugger to...
0
5577
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
6
3827
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 an object of type 'WindowsApplication10.Form1+DictionaryExt`2' was not found." Source="mscorlib" You can just past the snippet on any form with a button.
0
1017
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 = regex.Matches(txtInput.Text,@txtRegEx.Text,RegexOptions.IgnoreCase); foreach (Match NextMatch in matches) {
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.