Question

I am trying to clear only specific ID input field on my checkbox click. I am switching two fieldset on checkbox click and I want to clear specific ID fields from click on checkbox from all fieldset.

I got this script but its clearing everything from my form and that's what not I am looking but only specific ID fields.

function clear_form_elements(ele) {

    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'text':
            case 'textarea':
                $(this).val('');
                break;              
                this.checked = false;
        }
    });

}
Was it helpful?

Solution

I'm not sure what your form structure looks like, but if you want to clear text, textarea, checkbox, or select values by id you could use

$(ele).find('#id').val('');

Assuming:

1. the variable 'ele' represents the form jquery object. 
2. '#id' is the id of the field you want to remove the value for, and
3. that your javascript may be included on other pages that use the same '#id'

if '#id' is unique throughout your site, or your javascript is page-specific, Nick's method will work fine.

$('#id').val('');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top