Skip to content
Advertisement

How to do something before on submit?

i have a form which has a button that submits the form. I need to do something before submits happen. I tried doing onClick on that button but it happens after the submit.

I can’t share the code but, generally, what should I do in jQuery or JS to handle this?

Advertisement

Answer

If you have a form as such:

<form id="myform">
...
</form>

You can use the following jQuery code to do something before the form is submitted:

$('#myform').submit(function() {
    // DO STUFF...
    return true; // return false to cancel form action
});

Update; for newer JQuery versions (to avoid deprecation warnings), try:

$('#myform').on('submit', function() {

    // ...

    return true;
});
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement