Last Updated: February 25, 2016
·
1.603K
· 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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside