OK, I ‘m stumped. ?
My form allows the user to add and remove rows from a list of ID numbers (not to be confused with the ID in “getElementByID()”) and corresponding test tube letters, i.e. “n” rows of two input fields. The server technology being used is ColdFusion, which you need to know for just a few lines of code below. The code to do add/delete rows works fine. But if I add a row, when I loop through these rows as part of my validation at save time (via Javascript), it loops through and validates all but the last row — even though it recognizes that the last row is there
Here’s the code:
//Find out how many tubes there are in this batch.
//dyn_table_samples is the tbody name for the table containing the rows
var containerElement = document.getElementById(“dyn_table_samples”);
//Subtract 1 to eliminate column heading row and set the rows variable
var numRows = containerElement.childNodes.length – 1;
//This part converts a ColdFusion list of valid ID numbers so it can be used
//in Javascript. This works fine.
<CFOUTPUT>
var #toScript(ValidIDList, “jsIDList”)#;
</CFOUTPUT>
for (i=1; i<=numRows; i++) {
//get the element’s name.
var whichElement = “IDNumber”+i;
alert(whichElement);
var IDField = document.getElementById(whichElement);
//alert the field name and value. This works OK for all but the last row
alert(“i = ” + i + “; IDField = ” + IDField.value);
//See if the field’s value is in the list
if (jsIDList.indexOf(IDField.value) == -1) {
alert(IDField.value + ” is not a valid ID Number.”);
return;
}
}
Everything is fine; the outputs that I’m testing via the alert statements are fine, until I get to the last row. For some reason, I get an error when it performs getElementById() on the last row. It creates the “whichElement” variable, but that’s as far as it gets. Why wouldn’t it see that last row?
Anyone know why?