0

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.

2 Answers 2

2

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.
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
});

1 Comment

You can just use "this" instead of $(this).get(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.