[Javascript] Passing form name to function

Chris Tifer christ at saeweb.com
Wed Sep 3 08:21:42 CDT 2003


No need to use eval when the forms collection can, and in my opinion, should
be referenced.

fuction postForm(formName){
    var objForm = document.forms[formName]
    objForm.submit()
}

Of course I didn't work in any error detection but as long as you pass your
form names correctly, that is probably the preferred way of referencing
elements rather than a clunky eval statement.  Also, you can reference this
form more than 1 time now - say if you want to reference the elements
collection:

for(var x = 0; x < objForm.elements.length; x++){
    alert(objForm.elements[x].name + " = " + objForm.elements[x].value)
}

Hope that helps.

Chris Tifer
http://emailajoke.com



----- Original Message ----- 
From: "David Lovering" <dlovering at gazos.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Tuesday, September 02, 2003 9:04 PM
Subject: Re: [Javascript] Passing form name to function


> Hmmm... try the following
>
> function postForm(formName) {
>      if (formName == "") { return -1; }
>      eval('document.' + formName + '.submit()');
> }
>
> presuming of course you check to make sure the form actually exists, etc.
>
> I use this method all the time, and it has yet to bite me in the butt.
>
> -- Dave Lovering
>
> P.S:  A slightly more "artsy" method --
>
>   function postForm(formName) {
>       if (formName == "") { return -1; }
>       document.forms[formName].submit();
>   }
>
>   All this presumes too that the "action" elements are correctly
defined...
>
>
>
> ----- Original Message ----- 
> From: "Clancy Jones" <clancyjones at hotmail.com>
> To: <javascript at LaTech.edu>
> Sent: Tuesday, September 02, 2003 5:51 PM
> Subject: [Javascript] Passing form name to function
>
>
> > Apologies, my whole question is...
> >
> > Hi all, I have a page with several forms on it.  I want to have a
generic
> > javascript function for submitting them after validation.
> >
> > eg.
> > <script language="JavaScript">
> > function postForm(formName){
> > document.formName.submit();
> > }
> > </script>
> >
> > Then within a particular form I have a button eg:
> > <input name="Qsearch" type="button" value="Search"
> > onClick="postForm('nameOfThisForm');">
> >
> > At the moment this is giving me an error:
> > document.formName is null or not an object.
> >
> > Can someone help me out with the right syntax for this?
> >
> > TIA,
> > Clancy
> >
> > _________________________________________________________________
> > Download MSN Messenger @  http://messenger.xtramsn.co.nz   - add your
> > friends!
> >
> > _______________________________________________
> > Javascript mailing list
> > Javascript at LaTech.edu
> > https://lists.LaTech.edu/mailman/listinfo/javascript
> >
> >
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>




More information about the Javascript mailing list