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!

Submit form based on formname paramater?

Status
Not open for further replies.

dalin12

Programmer
Nov 4, 2004
2
US
Hello all..

here's what im trying to accomplish... i have 3-4 different forms on several pages... when each one is submitted, i want to be able to call a function that refernces that forms specific name, ex. document.form1.submit();

as opposed to using this.form, i thought i could accomplish it with something similar to:

<input type='image' src='img/button_go.gif' onclick='return showWaiting(form1);'>

function showWaiting(formname)
{
document.forms["formname"].submit();
}

OR

function showWaiting(formname)
{
document.formname.submit();
}

bottom line, is there a way to reuse a function, to submit multiple forms, and have the form name be sent as a paramater to the function, vs. hard coded...

thanks!
 
You can reuse the function and you're pretty close to it.

The document.forms[...] method is the way to go.
What you put between the square brackets is a string or string variable that is the same as the form name.
I'm guessing you got the idea right, but in the example you posted your document.forms["formname"] will look for a form with the name 'formname', if you want to use the string the function got passed as argument, simply drop the quotes: document.forms[formname].

For that to work the function must get passed an actual string as argument. In your example onclick='return showWaiting(form1);' passes the variable form1 to the function. I'm not sure, but I'm guessing you really want to pass the string "form1" as argument, 'form1' being the name of the first form your image is in.

Also, as far as I can see, there's no need for the return in your onclick.

Try something like this:
HTML
Code:
<input type='image' src='img/button_go.gif' onclick='showWaiting("form1");'>
Javascript
Code:
function showWaiting(formname)
  {
    document.forms[formname].submit();
  }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top