baghmg
Last Updated: February 25, 2016
·
1.176K
· moak

Resetting forms with jQuery

Surprised this is not part of the jQuery library

(function($){
    $.fn.reset = function(fn) {
        return fn ? this.bind("reset", fn) : this.trigger("reset");
    };
})(jQuery);

You can chain this function as expected and if you pass a function as argument you can do something whenever the form is reset via reset button or trigger.
Some simplified examples of use

//Scroll to top of screen when form is reset
$('#myForm').reset(function(){
    window.scrollTo(0,0);
});

//Reset the form when an icon is clicked
$('.icon-reset').click(function(){
    $(this).parents('form').reset();
});

https://gist.github.com/MarkVaughn/4975405