[Javascript] setting dynamic form values with dynamic data

Sean Moran sean at i2on.com
Wed Nov 24 11:31:49 CST 2004


Hello, looking for some advice.

I have a dynamic form that is quite complex when it comes down to 
formcheck time. I am hoping to clean up the javascript a bit, but I am 
kind of stuck on on bit...I think I have been dealing with the trees 
too long and can' t see the forest here.

The form is php genereated, and I am setting a hidden form field that 
has the number of "input sets", for lack of a better phrase. Input sets 
being a group of form elements that are related and need to be verified 
together - like if one field is filled in this other must be too, etc.

So in my formcheck script I am getting the number of sets ok, but now I 
need to dynamically set the value of several hidden form elements.

hard coded, it looks like this (if there were 5 input sets):

document.forms[0].file0data.value = 
someFunction(document.forms[0].elements[2].value);
document.forms[0].file1data.value = 
someFunction(document.forms[0].elements[9].value);
document.forms[0].file2data.value = 
someFunction(document.forms[0].elements[16].value);
document.forms[0].file3data.value = 
someFunction(document.forms[0].elements[23].value);
document.forms[0].file4data.value = 
someFunction(document.forms[0].elements[30].value);


To add to the problem, these form elements on the right have array 
style names (<input type="text" name="otherdata[0]" >) so that is why I 
was originally using the elements array value to reverence them. I have 
since discovered that I can use 
document.forms[0].elements["otherdata[0]"].value successfully, which is 
nice. So I am trying to do the above code in a for loop, without much 
luck:

(howmany = the number of input sets)

function linkEm(howmany) {
	for (i=0; i < howmany; i++) {
		field = "document.forms[0].file"+ i + "data.value";
		value = eval("document.forms[0].elements['otherdata["+i+"]'].value");
		field = value;
}


but these new dynamic form values are not being POSTed or GETed. Any 
ideas?

Thanks,




More information about the Javascript mailing list