Select2 reset to placeholder

I know this is repeated. I searched everywhere and I cannot see where my problem is.

I want to reset the selected value to placeholder.

I have a codepen running in case anyone wants to see the code.

I am using the last version of select2

http://codepen.io/GGarciaSeco/pen/WxNaGo?editors=1111

$(document).ready(function(){
 $(".select2").select2({
   placeholder:'A SELECT EXAMPLE',
   width:'100%'
  });

 $("#reset").on('click', function(e){
   console.log('e', e);
   var $select = $("#select")[0];
   console.log($select);
   $($select).select2('val', '');
  });
});

Upvotes: 1

Views: 1847

Answers (1)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Not sure what is causing you the problem here, because it should have just worked fine. But a little trick here could get you what you want. Just set original select element's value to empty and trigger its change event.

Ex:

$("#reset").on('click', function(e){
    $("#select").val('').trigger('change');
});

Updated Pen HERE

Upvotes: 3

Related Questions