Click to See Complete Forum and Search --> : list variable in document.forms... (newbie help!)
getho
07-01-2003, 05:03 AM
How come this works...
anVar = document.forms["bkform"].elements["street"].value;
but this doesn't
anVar = document.forms["bkform"].elements[fieldNames[i]].value;
or
anVar = document.forms["bkform"].elements[aVariable].value;
I guess anVar is looking for a form element called aVariable, but how do I
get it to look at a VARIABLE???
Thanks
Getho
getho
07-01-2003, 10:13 PM
thanks for replying Dave,
it didn't work! I tried every variation and it just kept not working. I eventually used a number instead of a variable - which worked much better for me anyway, but still confused why the variable didn't work.
Geth
Originally posted by getho
it didn't work!
What Dave said is accurate. If the form is named, "bkform" and the element is the second element in the form, named, "street" all of the below point to the "street" object in the form:
document.forms["bkform"].street
document.forms["bkform"].elements["street"]
document.forms["bkform"].elements[1]
document.bkform.street
document.bkform.elements["street"]
[J]ona
getho
07-02-2003, 06:21 PM
Hi Dave,
This is what I had, however I think you might have hit the nail on the head. This was in a function that was called from a submit button inside a form tag.
var fieldNames = new Array('o','street','city','state','phoneday','phoneother','people');
for(i=1; i<7; i++) {
tempVar = fieldNames[i]
document.forms["bkform"].tempVar.value
}
however what I have now is called from the same place and works fine:
for(i=1; i<7; i++) {
fValue = document.forms["bkform"].elements[3+i].value;
fName = document.forms["bkform"].elements[3+i].name;
alert ("fValue:"+fValue+" fName "+fName);
document.cookie = fName + "=" + fValue
}
??????? Getho
Your first function probably doesn't work because of the last statement in the for() loop. If you put alert(document.forms["bkform"].elements[tempVar].value); and change your for() loop to the following, the code you had should have worked. I've edited it to make it more dynamic.
var tempVar;
function getValues(f){
/* f = document.bkform, you would call this function onClick of a button like this:
<input type="button" onClick="getValues(this.form);">*/
for(i=0; i<f.elements.length; i++){
/* The for() loop starts at zero, because arrays begin at zero*/
tempVar = f.elements[i];
/* f.elements[i] refers to the object iself, so the below alert does not need to refer to the form; however, we could use f.elements[i].name and make the below alert(document.forms["bkform"].elements[tempVar].value), but that would just prolong the code */
alert(tempVar.value);
}
}
[J]ona
getho
07-02-2003, 10:38 PM
Thanks Jona, I'm attempting to get my head round your code :) (I'm new!) I still write out stuff in long hand - I only do javascript from time to time, and it really helps when I come back to it (I know, I know! I'll get better!)
Dave... errm... I've overwritten my original code and I REALLY hope that I didn't use that line! I think it was actually more like this..
anVar = document.forms["bkform"].tempVar.value;
But as you said before - is it possible that calling it before the </form> was the problem? And is the one that works still dodgy if its called before the form tag is closed?
Geth