Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function after a form is submitted using JavaScript / jQuery

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?

like image 315
Annapoorni D Avatar asked Nov 15 '12 08:11

Annapoorni D


People also ask

How do you call a function when a form is submitted?

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.

How do I keep form data after submitting the form using JavaScript?

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.

Which event is triggered when a form is submitted?

The submit event fires when a <form> is submitted.

How do you check if a form is submitted in jQuery?

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.


1 Answers

$("#myFormId").on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: $(this).prop('method'),
        url : $(this).prop('action'),
        data: $(this).serialize()
    }).done(function() {
        doYourStuff();
    });
});
like image 93
adeneo Avatar answered Sep 22 '22 14:09

adeneo