Is there an "after submit" jQuery option?

polyhedron picture polyhedron · Mar 2, 2011 · Viewed 78k times · Source

I have a form that uploads a file and targets an iframe on the page. When the user clicks submit, I want the file contents to "clear" out.

I tried this

$('#imageaddform').submit(function(){
    $('#imagefile').val('');
});

But it clears the form before the submit, so nothing is ever uploaded.

Is how do I clear after submit?

Answer

lonesomeday picture lonesomeday · Mar 2, 2011

If you have no other handlers bound, you could do something like this:

$('#imageaddform').submit(function(e) {
    e.preventDefault(); // don't submit multiple times
    this.submit(); // use the native submit method of the form element
    $('#imagefile').val(''); // blank the input
});