Multiple submit buttons in the same form with onsubmit function

I have 3 sumbit buttons in myform and I need 3 different actions based on which he clicked. so i need to write a javascript function to do the same. how can i find out in javascript the button is clicked.

JavaScript:

<script type="text/javascript" language="javascript"> function submitform(){ //do something } 

HTML:

 form name="myform" method="get,post" onsubmit="return submitform();" input type="submit" name="submit" value="Home" input type="submit" name="submit" value="Reschedule" input type="submit" name="submit" value="Cancel" 

Any help would be appreciated

+4
source share
4 answers

If you can use jQuery, this can be a lot easier.

One solution would be to remove the submit () form from the form and add it to the onclick event of each of your submit buttons. So you can change it to pass a parameter indicating which button called it.

Good luck.

0
source

Edit:

You can also have hidden input that tells you which button was clicked, and then process it on the server. When a button is pressed, it will change this input before sending.

 <input type="submit" onclick="javascript:document.getElementById('submitClicked').value='forward';return true;" id="linkName" value="Forward" /> <input type="submit" onclick="javascript:document.getElementById('submitClicked').value='back';return true;" id="back" value="Back" /> 

Demo: http://jsfiddle.net/Homeliss/vperb/

Note. . demo uses jquery to display a message instead of posting a form, but it's just for demo purposes. The solution is simple javascript

0
source

we can submit the form once on the html pages. Therefore, we use only one submit button on the form. But to call additional functions, we can use the onClick event, and the input type is the button.

0
source

I have a question. is there another input field inside your form? If there is another field, such as a text field, what button actions will be called up when we press Enter inside the text field?

My suggestion is as follows:

 <form name="myform" method="get,post" onsubmit="return false;"> <input type="button" value="Home" onclick="submitform(1)" /> <input type="button" value="Reschedule" onclick="submitform(2)" /> <input type="button" value="Cancel" onclick="submitform(3)" /> </form> 

in this code, the user must click on the button to submit the form, and pressing the enter key will not lead to any action.

-1
source

All Articles