How do I perform an action immediately after an <input type="reset"/>
has already reset the form elements?
Asked
Active
Viewed 2.5k times
7

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
-
2It'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 Answers
15
Try :
<input type="reset" onclick="return resetForm();"/>
function resetForm(){
setTimeout(function(){
// do whatever
}, 50);
return true;
}

ChristopheCVB
- 7,269
- 1
- 29
- 54
-
3
-
-
Looks like this is the only way to run something really after the reset – Patricio Rossi Apr 25 '17 at 13:02
-
actually, you don't need the 50 ms, just the timeout is deferring this process to the end of the js processing pile. – Felipe Quirós Nov 16 '17 at 21:34
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
-
4
-
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. – Patricio Rossi Apr 25 '17 at 13:00
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