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();
});
Written by Mark Vaughn
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#