Skip to content

Instantly share code, notes, and snippets.

@inferno7291
Last active February 27, 2018 08:53
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 inferno7291/ac0ad9478750c722e050be1cd7a59614 to your computer and use it in GitHub Desktop.
Save inferno7291/ac0ad9478750c722e050be1cd7a59614 to your computer and use it in GitHub Desktop.
Clear form javascript/jquery
/**
* 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