PDA

View Full Version : help - can anyone help me to get this code snippet to work


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

Philip M
05-08-2008, 07:13 AM
mainform is the name of an object passed to the function. You want the value of that object.

function populatemainform(mainform) {
mainform = mainform.value;

Likewise
for (var e = 0; e < document.forms[f].elements.value.length; e++)

lawrencef
05-09-2008, 01:54 AM
thanks guys, seems to be working. just on the note of "concatenating variables into js statements" (again assuming im describing what this is accurately) . in vba you can enter a variable into a statement by using the following characters " & on each end of the variable. for example

PivotFields(index:="" & pivfieldnam & "").DataRange.Select

in place of PivotFields(index:="income").DataRange.Select

does anyone know the syntax to do similar in js.

tomws
05-09-2008, 03:21 AM
thanks guys, seems to be working. just on the note of "concatenating variables into js statements" (again assuming im describing what this is accurately) . in vba you can enter a variable into a statement by using the following characters " & on each end of the variable. for example

PivotFields(index:="" & pivfieldnam & "").DataRange.Select

in place of PivotFields(index:="income").DataRange.Select

does anyone know the syntax to do similar in js.

If I understand what you're asking, this html example should explain it:
// text example
var myVar = "CHEESE";
var someText = "My variable has the value " + myVar + " stored in it!";
// someText: My variable has the value CHEESE stored in it!


Is that what you're asking?

lawrencef
05-12-2008, 02:07 AM
im note sure if this is what im after - i can see that using the characters " + in a string will insert the required variable ie as you have shown

var someText = "My variable has the value " + myVar + " stored in it!";

i guess what im asking is does this syntax work if you are trying to enter a variable in a piece of operating code rather than just entering it into a string like you have shown above.

im not sure if and when anyone would need to do this in js but i know that knowing how to do it has come in handy at times in vba

for example if you had a line of code funtions such as

document.forms[applicant1].elements[name].value

if you wanted to substitute a specific part of this line (ie the middle part of the line enclosed in the following quotation marks document."forms[applicant1].elements[1]".name

with a string variable such as var alt1= ".div[somename].image[3]"


my question is - how can you write the syntax for this to make it work ie would the following line work and return the name of image[3] in the div named somename rather than the name of element[1] in the form name applicant1

document.+" alt1 "+.name

or would the complier simply interpret this entire line as a string and not return the name im after.