s.k.paul
s.k.paul

Reputation: 7291

resetting all select2 jquery select elements

I need to reset couple of select2 select element. Currently i am doing it as follows

            $("#ItemId").select2("val", "");
            $("#PermanentDistrictId").select2("val", "");
            $("#PermanentThanaId").select2("val", "");
            $("#PresentDistrictId").select2("val", "");
            $("#PresentThanaId").select2("val", "");
            $("#OccupationId").select2("val", "");
            $("#GenderId").select2("val", "");

But i would like to do it in single function like as follows-

ParticularForm.AllSelectElements.select2("val", "");

I am asking here as because my javascript skill is very poor. So, I need your help.

Thank you.

Upvotes: 2

Views: 4611

Answers (3)

Mantas Dainys
Mantas Dainys

Reputation: 91

Just trigger the change.select2 event with .trigger('change.select2')

$(document).on('click','[type="reset"]', function (e){
                $(this)
                    .closest('form')
                    .find('select')
                    .val(null)
                    .trigger('change.select2');
            });

Upvotes: 1

Aba
Aba

Reputation: 614

$(".select2-hidden-accessible").val(null).trigger("change");

Upvotes: 6

gen_Eric
gen_Eric

Reputation: 227190

Your ParticularForm.AllSelectElements can be done as follows:

$('#yourform select').select2("val", "");

This selects all <select> elements inside #yourform.

Upvotes: 3

Related Questions