Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 onsubmit event without adding code to the buttons themselves?


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

like image 451
GetFree Avatar asked Aug 21 '10 17:08

GetFree


5 Answers

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" />
  </form>

  <script type="text/javascript">

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

  </script>

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.
like image 116
naikus Avatar answered Nov 10 '22 05:11

naikus


Does having an event listener on each button count as adding code? Otherwise, there's no way to see what button triggered the submit event, unless you want to get down and dirty and calculate the mouse position during the event and compare it to button positions.

Otherwise, the next best thing would be to assign an event handler for the click event of button and assign the name of that button to a variable you can check in the form's submit event.

like image 29
Cristian Sanchez Avatar answered Nov 10 '22 06:11

Cristian Sanchez


There are a couple of ways that I can think of.

You can use different values, and your unobtrusive javascript can help with it.

One discussion on this approach (using different values for each button) is here:

http://www.chami.com/tips/internet/042599I.html

I tend to go with using different name attributes for each button.

A blog on that is here: http://www.codetoad.com/javascript/multiple.asp

I don't follow either of these, which approach will work best is going to depend on different factors, such as, are you handling the submit buttons in javascript, or will the server get the form, then have to figure out what the user wanted.

Personally, I prefer to use the ajax approach, now, where I just attach events to the buttons after the page is loaded, using unobtrusive javascript, and then based on the user choice call out to the correct function, but that depends on whether you can add a script link to the html page.

UPDATE:

In order to do this with javascript, the simplest way is to attach an event on the click of the button, and then look at the name to decide how to handle it.

In actuality, the form never truly has to be submitted to the server, but you can handle everything in the background by wrapping up the parameters (options) and sending them to the server, and let the user know the results.

like image 34
James Black Avatar answered Nov 10 '22 06:11

James Black


There is a submitter attribute on SubmitEvent object.

See example:

<!DOCTYPE html>
<html>
<body>
<form action="./test.html" onsubmit="myFunction(event)">
  Enter name: <input type="text" name="fname">
  <button id="firstButton" type="submit">Button 1</button>
  <button id="secondButton" type="submit">Button 2</button>
</form>

<script>
function myFunction(event) {
  // This should log id of the button that was used for submition
  console.log(event.submitter.id); 

  // Prevent sending the form (just for testing)
  event.preventDefault();
}
</script>

</body>
</html>

See https://developer.mozilla.org/en-US/docs/Web/API/SubmitEvent/submitter

like image 2
Peter Parker Avatar answered Nov 10 '22 06:11

Peter Parker


Sorry to warm up this very old thread. The question hadn't been answered here but only approaches for practical workarounds have been given here before.

But the event-object does carry information about which object has initiated it. In a handler b4submit(e) you can get the submit-button like this:

function b4submit(e) {
    var but = e.originalEvent.explicitOriginalTarget;
    ...
}

And but is an HTML Object with all the attributes you've assigned it with, like name, id, value etc.

I came across this after some debugging, and I thought it might be of a wider interest as a clean solution for this issue.

like image 1
NoPro Avatar answered Nov 10 '22 05:11

NoPro