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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing to object using dynamic varible 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have a script
Code:
function postComments(formname,id) {
	var msg='';
	if (document.forms.formname.aTextArea.value == '') msg = 'Please fill in comments.\n';
	if (document.forms.fiformnamele.gotChaString.value == '') { msg = msg + 'Please fill in 5 digits.\n'; }
	var comments = document.forms.formname.aTextArea.value;
	var gotCha = document.forms.formname.gotChaString.value;

I am trying to use the same function from multiple pages where the form has different name.

I was hoping that using above code method would work but I get the obvious error "document.forms.formname is undefined"

What must I do to be able to use a variable in lieu of literally typing the name of the form itself?

Thank you all in advance for your assistance!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
[1] Change every
[tt] [/tt]document.forms.formname
to
[tt] document.forms[formname][/tt]

[2] What is "fiformnamele" in [tt]document.forms.fiformnamele[/tt]? if it means formname "variable", do the same change. If it is a literal name of some form on the body, you can leave it like that or write it like this - that's the idea of the equivalence.
[tt] document.forms["fiformnamele"][/tt]
 
@tisuji
[2] What is "fiformnamele" in document.forms.fiformnamele? if it means formname "variable", do the same change. If it is a literal name of some form on the body, you can leave it like that or write it like this - that's the idea of the equivalence.

This is simply a bad cut and paste ... Thanks for bringing that to my attention! :)


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top