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:
JavaScript
x
4
1
<form id="myform">
2
3
</form>
4
You can use the following jQuery code to do something before the form is submitted:
JavaScript
1
5
1
$('#myform').submit(function() {
2
// DO STUFF...
3
return true; // return false to cancel form action
4
});
5
Update; for newer JQuery versions (to avoid deprecation warnings), try:
JavaScript
1
7
1
$('#myform').on('submit', function() {
2
3
// ...
4
5
return true;
6
});
7