views:

193

answers:

1

I want a jQuery form submit handler to respect any previous submit handlers, including ones added with onsubmit.

I'm trying to detect the previous handler's return value but can't seem to do it:

<form><input type="submit" /></form>
<script type="text/javascript">
$('form')[0].onsubmit = function() { return false; }; // called first
$('form').submit(function(e) {
  console.log(e.result); // undefined
  console.log(e.isDefaultPrevented()); // false
  console.log(e.isPropagationStopped()); // false
  console.log(e.isImmediatePropagationStopped()); // false
});
</script>

Is there a way to do this?

A: 

I found one non-jQuery way to do this:

var form = $('form')[0];
var old_onsubmit = form.onsubmit;
form.onsubmit = function() {
  if ($.isFunction(old_onsubmit)) {
    if (old_onsubmit() === false) {
      console.log("false");
      return false;
    }
  }
  console.log("true");
  return true;
}

But I'd much prefer detecting this from the jQuery-bound submit handler

orip
<sigh> no suggestions for a few weeks now, accepting my own answer :(
orip