I need to access the values of a number of fields in my page and perform the same calculations on them. I would like to do something like this, allowing me to dynamically access different form elements, but I cannot get it to work:
function numInFamily(famID) {
var numAdults;
var numChildren;
var adultFieldID;
var childFieldID;
adultFieldID = 'numAdultsFam' + toString(famID);
childFieldID = 'numKidsFam' + toString(famID);
numAdults = parseInt(document.getElementById(adultFieldID).value,10);
numChildren = parseInt(document.getElementById(childFieldID).value,10);
return numAdults + numChildren;
}
Could anyone explain how I can dynamically reference these elements?