Tuesday, 24 February 2015

Clear All Input Fields in a Specific div With JQuery

I have added two buttons in the Fiddle to illustrate how you can insert or clear values in those input fields through buttons. You just capture the onClick event and call the function.


//Fires when the Document Loads, clears all input fields
$(document).ready(function() {
  $('.fetch_results').find('input:text').val('');    
});


//Custom Functions that you can call
function resetAllValues() {
  $('.fetch_results').find('input:text').val('');
}


function addSomeValues() {
  $('.fetch_results').find('input:text').val('Lala.');
}


This function is used to clear all the elements in the form including radio button, check-box, select, multiple select, password, text, textarea, file.


function clear_form_elements(class_name) {
  jQuery("."+class_name).find(':input').each(function() {
    switch(this.type) {
        case 'password':
        case 'text':
        case 'textarea':
        case 'file':
        case 'select-one':
        case 'select-multiple':
            jQuery(this).val('');
            break;
        case 'checkbox':
        case 'radio':
            this.checked = false;
    }
  });

}

No comments:

Post a Comment