JavaScript form value with variable field name

advertisements

simple javascript but can't seem to get it to work.

var number = 5;
var netiteration = "net"+number;  // makes netiteration now equal net5

var formvalue = document.forms.myformname.netiteration.value;

why doesn't this get the value of the form field with the name/id of "net5", in the form "myformname"?

also, I'm working from a 10 year old javascript book, so maybe the syntax has changed?

thanks


Try:

var number = 5;
var netiteration = "net"+number;  // makes netiteration now equal net5

var formvalue = document.forms.myformname[netiteration].value;

Your original code was looking for a field called "netiteration" but you want the field that has a name equal to the evaluated value of netiteration.