I'm working on a function validate(), which will validate data entry in a <form>. The function loops through the form's elements collection. For each element, another function - doSomething() - is called.
I tried to pass a reference to the form as an argument for the second function, but I couldn't seem to access the elements collection.
Then I tried passing the name of the form, and establishing a separate reference to the form as per the code that follows, but again, cannot reference the elements collection.
As I'm a raw novice, I'm probably missing something really obvious, if you can make any suggestions I'd welcome them.
Max Hugen
Australia
I tried to pass a reference to the form as an argument for the second function, but I couldn't seem to access the elements collection.
Then I tried passing the name of the form, and establishing a separate reference to the form as per the code that follows, but again, cannot reference the elements collection.
As I'm a raw novice, I'm probably missing something really obvious, if you can make any suggestions I'd welcome them.
Code:
function validate(formID) {
// Get a ref to the form
var x=document.getElementById(formID);
// Do something with each element in the form
for (var i=0;i<x.length;i++) {
doSomething(formID,i);
}
}
function doSomething(formID,i) {
// Tried passing a ref to the form from the
// validate() function above, didn't work, so
// trying a separate ref, using the formID.
var x=document.getElementById(formID);
with (document) {
// The form's properties can be accessed:
write('The forms name is: ' + x.name);
write('The element index number we passed is: ' + i);
// BUT the form's elements collection can't
// be accessed, as this doesn't work:
write('The elements name is: ' + x.elements[i].name);
// In fact the code stops running as soon as the
// elements collection is referenced.
}
return;
}
Max Hugen
Australia