I want to call a function after a form is submitted, I see we can do this in jQuery with .submit(handler function())
but the method description says, the handler method will be executed just before the form is submitted. How can I actually attain this? Should I use setTimeout
after the form is submitted or is there any other solution for this?
To call and run a JavaScript function from an HTML form submit event, you need to assign the function that you want to run to the onsubmit event attribute. By assigning the test() function to the onsubmit attribute, the test() function will be called every time the form is submitted.
To keep the values, you must fill in the values on the server while rendering the page. Usually, you can simply copy the data from the HTML request parameters into the fields. Save this answer.
The submit event fires when a <form> is submitted.
jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.
$("#myFormId").on('submit', function(e) {
e.preventDefault();
$.ajax({
type: $(this).prop('method'),
url : $(this).prop('action'),
data: $(this).serialize()
}).done(function() {
doYourStuff();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With