views:

29

answers:

1

Given the following HTML file:

<!DOCTYPE html5>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function() {
    $('#form').submit(function(e) {
      // put your breakpoint here to look at e
      alert('Which button was clicked?');
    });
  });
</script>
</head>
<body>
  <form id="form">
    <input name="text" type="text"><br/>
    <input name="commit" type="submit" value="Save"/>
    <input name="commit" type="submit" value="Cancel"/>
  </form>
</body>

I need to know which button was clicked by the submit handler method. Using the $.click handler WILL NOT DO. If I can't do it using the $.submit handler, then just say so.

+2  A: 

Try the following:

$('#form').submit(function(e) {
    console.log(e.originalEvent.explicitOriginalTarget);
});

This one is Gecko-only, so you should probably search for other solutions. I do not know of one, though.

elusive
Ok, +1 but that only works with FireFox (Gecko).
DJTripleThreat
@DJTripleThreat: I do not think that there is something like a cross-browser version of this. This property is meant to be used by Firefox addon coders (as far as i read).
elusive