Skip to content

Instantly share code, notes, and snippets.

@LindseyWhitney
Created November 3, 2015 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LindseyWhitney/afdd35c130965131c193 to your computer and use it in GitHub Desktop.
Save LindseyWhitney/afdd35c130965131c193 to your computer and use it in GitHub Desktop.
Clears various types of form input.
//jQuery Plugin
$.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;
});
};
// Call it on your <form>
$('form').clearForm();
@LindseyWhitney
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment