8

How do I perform an action immediately after an <input type="reset"/> has already reset the form elements?

5
  • How about adding a onclick handler to the input? Commented Jul 18, 2011 at 20:48
  • HTMLFormElement.onreset will work nicely. Link: w3.org/TR/DOM-Level-2-Events/… Commented Jul 18, 2011 at 20:53
  • 2
    It's generally bad practice to include a reset button at all. Users often click them accidentally and lose all their work. In practice, it's very rare that anyone really wants to reset the form. Commented Jul 18, 2011 at 20:53
  • @Mrchief: That fires before, not after. @Matt: Yup, thanks! @jimbojm: Yeah but the person who wants this page says he wants a reset button, so that's not much of a choice on my part. :P Thanks though. Commented Jul 18, 2011 at 21:24
  • @jimbojw? Yours is a very bold statement. You might be interested in this discussion on the UX StackExchange community about reset buttons. Commented Nov 16, 2018 at 8:49

4 Answers 4

16

Try :

<input type="reset" onclick="return resetForm();"/>

function resetForm(){
    setTimeout(function(){
        // do whatever
    }, 50);
    return true;
}
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for delaying the function to fire after the form was reset.
For every browser I tested, setting the timeout to 0 was sufficient.
Looks like this is the only way to run something really after the reset
actually, you don't need the 50 ms, just the timeout is deferring this process to the end of the js processing pile.
The answer could be improved by adding some explanation about why it works.
5

Forms have a reset event that you can listen for.

<script>
function resetHandler() {
    // code
}
</script>
<form ... onreset="resetHandler();">
</form>

Of course, it's bad practice to add javascript handlers this way, so you'd want to use .addEventListener/.attachEvent or jQuery.bind(), but you get the idea.

2 Comments

resetHandler runs before the form is reset, not after!
The questions is about the execution of a function AFTER the reset is done, not before, not at the same time. this answer is not correct.
2

Write code/events which you wanted to call in middle of this function. I have tested this. Working good.

$(document).ready(function() {
    $("input:reset").click(function() {       // apply to reset button's click event
        this.form.reset();                    // reset the form

        // call your functions to be executed after the reset      

         return false;                         // prevent reset button from resetting again
    });
});

1 Comment

The reset function fails when a form control has the name or id 'reset'
-1

You always can use jQuery, or do some tricks with form reset event itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.