Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear select2 dropdown?

I am working on a requirement that I have to clear a select2 dropdown box after form submission through ajax. I have searched & tried few solutions but didn't work though.

my select2 code is:

$(document).ready(function(){
 $("#my_select_id").select2({
      placeholder: "Select One Option",
      allowClear: true,
      initSelection: function(element, callback) { }
 }); 
})

Script to clear select2 is:

$('#my_select_id').select2("val", "");

Could anyone please tell what's wrong in this code, Thanks.

like image 431
Prasad Patel Avatar asked Mar 22 '17 12:03

Prasad Patel


2 Answers

Try this: $('#your_select_input').val('').trigger('change');

like image 68
Leo Schoberwalter Avatar answered Sep 28 '22 13:09

Leo Schoberwalter


Try the following code instead of val use data it will work

$("#my_select_id").select2('data', null)
like image 26
AdamIJK Avatar answered Sep 28 '22 12:09

AdamIJK