7

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

user541686
  • 205,094
  • 128
  • 528
  • 886
  • How about adding a `onclick` handler to the input? – Mrchief Jul 18 '11 at 20:48
  • HTMLFormElement.onreset will work nicely. Link: http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents –  Jul 18 '11 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. – jimbo Jul 18 '11 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. – user541686 Jul 18 '11 at 21:24
  • @jimbojw? Yours is a very bold statement. You might be interested in [this discussion](https://ux.stackexchange.com/a/1070/116190) on the UX StackExchange community about reset buttons. – mapto Nov 16 '18 at 08:49

4 Answers4

15

Try :

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

function resetForm(){
    setTimeout(function(){
        // do whatever
    }, 50);
    return true;
}
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
6

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.

digitalbath
  • 7,008
  • 2
  • 17
  • 15
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
    });
});
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • The [reset function](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset) fails when a form control has the name or id 'reset' – Michael Plotke Oct 20 '15 at 14:16
-1

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

caarlos0
  • 20,020
  • 27
  • 85
  • 160