tags:

views:

143

answers:

2

I've got a form with an onsubmit attribute like this (which I can't change):

<form onsubmit="return validate(this);">

I want to run another function, that needs to look at the return result of validate() and then possibly return false to prevent the form submitting.

Edit:

The contents of onsubmit="" could change, so I can't just write that code inside my own function.

+1  A: 

Came up with the following while writing the question, but I'm open to a better way:

// Grab the old function and remove it from the form
var oldSubmit = $('form').get(0).onsubmit;
$('form').get(0).onsubmit = null;

$('form').submit(function()
{
    // Use .call to make sure "this" is correct when the old function is run
    if (!oldSubmit.call($(this).get(0)))
     return false;

    // Do stuff

    return false;
});
Greg
You can just use "this" instead of $(this).get(0)
Josh Stodola
@Josh Stodola Good point
Greg
+2  A: 

You could replace the validate function with your own with something like this:

var oldValidate = validate;

var validate = function(form) {
  var value = oldValidate(form);
  //do your stuff here.
}
Doomspork