2

How to clear form elements value within specific div using jQuery, i have more than 25 fields in my form, i need function which is clear all type of form elements value. I tried with the basic jQuery function

$('#div_id input[type="text"]').val('');

1
  • 1
    $('#div_id *').val(''); Commented Oct 17, 2014 at 6:55

2 Answers 2

3

hi you can use the below code to solve your problem, I hope it is working

function clear_form_elements(id_name) {
  jQuery("#"+id_name).find(':input').each(function() {
    switch(this.type) {
        case 'password':
        case 'text':
        case 'textarea':
        case 'file':
        case 'select-one':       
            jQuery(this).val('');
            break;
        case 'checkbox':
        case 'radio':
            this.checked = false;
    }
  });
}
Sign up to request clarification or add additional context in comments.

Comments

1

For all kind of input fields(including select and textarea) try the :input-selector

$('#div_id :input').val('');

Your selector will clear only those input element which as an explicit type="text" set, meaning <input name="abc" /> will not be cleared

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.