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 button

Status
Not open for further replies.

transtech

Programmer
Aug 21, 2001
23
US
Hi all,

I have a form with 3 submit buttons, i want to use javascript to detect which button was clicked and based on that i'll do something else. Can some one please tell me how to do it?

thanks,

transtech
 
instead of using subimt buttons, just use buttons:

<input type=button name=firstButton onclick=&quot;someJavascriptFunctionName()&quot;>
<input type=button name=firstButton onclick=&quot;someOtherJavascriptFunctionName()&quot;>
<input type=button name=firstButton onclick=&quot;yetAnotherJavascriptFunctionName()&quot;> theEclipse
eclipse_web@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
TransTech,

Eclipse is right, change your submit buttons to standard buttons and just issue the document.formname.submit() method in your function if and when you want to submit the form. More specifically on how to determine which button was clicked.
Code:
<input type=&quot;button&quot; name=&quot;b1&quot; value=&quot;Button 1&quot; onClick=&quot;yourFunction(1);&quot;>
<input type=&quot;button&quot; name=&quot;b2&quot; value=&quot;Button 2&quot; onClick=&quot;yourFunction(2);&quot;>
<input type=&quot;button&quot; name=&quot;b3&quot; value=&quot;Button 3&quot; onClick=&quot;yourFunction(3);&quot;>
At this point, it's irrelavant what you name the buttons or supply as the value or button text. Assuming all three buttons are calling the same function, your function still won't know which one was clicked unless you pass a parameter to the function.

See where we pass a parameter to the function with a number myFunction(buttonnumber). Then your function could look like this.
Code:
function myFunction(bn) {
  if (bn == 1) {
    // DO SOMETHING FOR BUTTON 1
  } 
  if (bn == 2) {
    // DO SOMETHING FOR BUTTON 2
  } 
  if (bn == 3) {
    // DO SOMETHING FOR BUTTON 3
  } 
}

Hope that provides some further help.

ToddWW :)
 
HI theEclipse,

What about if i must use it, is there a way??? Because i also need to check if user have check the selection box or not? I just want to know if there's a way that u can do it. please let me know.


thanks,
evan
 
if you are going to have one function to parse for the numbers, a switch is usually cleaner:

function myFunction(bn){
switch(bn){
case 1:
//do something for button 1
break;
case 2:
//do something for button 2
break;
case 3:
//do something for button 3
break;}
} theEclipse
eclipse_web@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top