GetFree GetFree - 2 years ago 92
Javascript Question

How to know which submit button fired the onsubmit event

When you have more than one submit button in a form, is there a way to know which one fired the

event without adding code to the buttons themselves??

Edit: I need to do the check on the client-side, i.e. with JavaScript.

Answer Source

The "submit" event is not fired by the button, but its fired by the "form". A quick test proves this:

  <form id="myform">
     <input id="email" type="text" value="1st Email" />
     <input id="action1" type="submit" value="Action 1" />
     <input id="action2" type="submit" value="Action 2" />

  <script type="text/javascript">

     document.getElementById("myform").onsubmit = function(evt)  {
        var event = evt || window.event;
        alert(; // myform
        alert(; // action2 (if action2 was clicked)
                                                // but only works in firefox!


Although in firefox, you can use event.explicitOriginalTarget property on event to get the input (submit) that was clicked causing the submit event to be fired. (if you want to know)

So best options for you are:

  • Have a different value to your submit buttons OR
  • Have those as normal buttons and click handlers to them via javascript.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download