Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval function not working in Netscape (works in IE)

Status
Not open for further replies.

JoliM

Programmer
Jun 25, 2001
20
US
I have a function that pulls the form element names on submit to validate the fields. The form is built dynamically so I don't know which of the 58 element names might be on the form. I'm looping through the element array to pull the name, then checking if the value has data. This works like a charm in IE but (of course) not in Netscape. I get an error stating "missing variable name" indicating the int variable I thought I declared:
var int = eval('document.forms[0].'+FieldName);

Here's the code
Code:
for (i = 0; i < document.forms[0].elements.length; i++) {
	var FieldName = document.forms[0].elements[i].name;
	var int = eval('document.forms[0].'+FieldName);
	if(int.value!=''){
          then do stuff
        }
}
Thanks,
JoliM
 
instead of getting the name from the elements array, and them using eval to handle the name, why don't you just do this?
Code:
for (i = 0; i < document.forms[0].elements.length; i++) {
    if(document.forms[0].elements[i].value!=''){
          then do stuff
        }
}
or, if you really want to get the field name, treating the elements array associatively and using the field name to index it should also work:
Code:
for (i = 0; i < document.forms[0].elements.length; i++) {
    var FieldName = document.forms[0].elements[i].name;
    if(document.forms]0].elements[FieldName].value!=''){
          then do stuff
        }
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Tracy,

Thanks for the quick reply. I am using &quot;document.forms[0].elements&quot; in a later statement so wanted to assign the object to a variable to make it easier. I liked your suggestion of using document.forms[0].elements[FieldName] instead of the eval statement I was trying to use. It is much cleaner your way.

Incidentally, the problem was that Netscape didn't like my variable named &quot;int&quot;. When I renamed the variable the script worked fine.

Thanks,
Joli
 
Anyone want to bet that int is a RESERVED WORD???

(Maybe I should add that warning to my signature. And Todd could add his comment about not being able to modify or close the main browser window to his.)
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top