Created
October 17, 2012 19:52
-
-
Save michaelminter/3907724 to your computer and use it in GitHub Desktop.
Clears a form with jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $('form').clearForm() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.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