Skip to content

Instantly share code, notes, and snippets.

@michaelminter
Created October 17, 2012 19:52
Show Gist options
  • Select an option

  • Save michaelminter/3907724 to your computer and use it in GitHub Desktop.

Select an option

Save michaelminter/3907724 to your computer and use it in GitHub Desktop.
Clears a form with jQuery
$('form').clearForm()
$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = '';
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
else if (tag == 'select')
this.selectedIndex = -1;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment