Clear Values of All Form Elements Using jQuery

I always wanted to have one common function to reset all form elements. Please have a look at jQuery code that does the same thing. It finds out all element of form and loops over it and reset them. I hope this thing will be helpful specifically where there are many search fields and you need to reset them frequently.

[code:js]
function resetFormElements(frm){
$(frm).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;
}
});
}
[/code]

Note : I noticed that some time in IE selectbox is not getting cleared. So you can modify script and put [code:js]’$(this).attr("selectedIndex",0);'[/code] for ‘select-one’ case. It will work in both IE and mozilla.

 

Demo