Last active
February 27, 2018 08:53
-
-
Save inferno7291/ac0ad9478750c722e050be1cd7a59614 to your computer and use it in GitHub Desktop.
Clear form javascript/jquery
This file contains 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
/** | |
* Limpiar el formulario | |
*/ | |
function clearForm(id_class_form) { | |
$(id_class_form).find(':input').each(function() { | |
switch(this.type) { | |
case 'password': | |
case 'select-multiple': | |
case 'select-one': | |
case 'text': | |
case 'textarea': | |
$(this).val(''); | |
break; | |
case 'checkbox': | |
case 'radio': | |
this.checked = false; | |
} | |
}); | |
} | |
$.fn.clearForm = function() { | |
$(this)[0].reset(); | |
$(this).trigger("reset"); | |
}; | |
$.fn.clearFormAll = function(hidden = true) { | |
return this.each(function() { | |
var type = this.type, tag = this.tagName.toLowerCase(); | |
if(!this.classList.contains('no_reset')){ | |
if (tag == 'form' || tag == 'div') | |
return $(':input',this).clearFormAll(hidden); | |
if (type == 'text' || type == 'password' || tag == 'textarea' || (type == 'hidden' && hidden)) | |
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