Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove select2 from dom

Say we have a simple select2 list:

<select id="e1">   <option value="AL">Alabama</option>     ...   <option value="WY">Wyoming</option> </select> 

Initiated like:

$("#e1").select2(); 

How can I remove select2 and return it to a regular dropdown? I can't find any examples or entry in documentation.

Something like:

$("#e1").select2('remove');  

would be nice.

like image 252
jdennison Avatar asked Oct 11 '13 22:10

jdennison


People also ask

How do I get rid of select2?

In order to enable or disable Select2, you should call . prop('disabled', true/false) on the element. Support for the old methods will be completely removed in Select2 4.1.

How do I stop select2 from auto selecting the first option?

This behaviour exists not because Select2 is forcing it, but because the browser is auto-selecting the first option for you. We can't control that, we can only work around it. Your best option is to use the placeholder support in Select2 and include a blank option tag as your default selection.

How do I hide select2 search box?

In order to hide the search box in select2, you can hide the search box by setting "minimumResultsForSearch" to a negative value. $('select'). select2({ minimumResultsForSearch: -1 });

How do I know if select2 is open?

select2:open is fired whenever the dropdown is opened. select2:opening is fired before this and can be prevented. select2:close is fired whenever the dropdown is closed. select2:closing is fired before this and can be prevented.


2 Answers

You need to use destroy method on select2. See the Documentation

i.e

 $("#e1").select2('destroy');  
like image 140
PSL Avatar answered Sep 18 '22 21:09

PSL


That work for me!

var $select = $('.select2').select2(); //console.log($select); $select.each(function(i,item){   //console.log(item);   $(item).select2("destroy"); }); 
like image 21
jcmordan Avatar answered Sep 21 '22 21:09

jcmordan