promotion

Monday, February 27, 2012

clearing form elements using jQuery

HTML form can be reset using reset button but it resets the form back to its original state, whereas you may want to clear all the fields in the form. Here i am going to show how we can clear form elements using jQuery.
function resetForm(ele){
    $(ele).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;
                break;
         }
     });
};
Above code find all the input fields under form/div and clear its values. This function can also use to clear set of input fields under #id.

No comments:

Post a Comment