58

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.

Zachary Espiritu
  • 937
  • 7
  • 23
jdennison
  • 2,000
  • 2
  • 15
  • 22

4 Answers4

129

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

i.e

 $("#e1").select2('destroy'); 
PSL
  • 123,204
  • 21
  • 253
  • 243
  • 8
    Link has changed (they replaced it with docs for 4.0 which are currently woefully incomplete). Passing explanation can be found here: http://select2.github.io/select2/#lifecycle – Kat Jan 07 '15 at 02:31
15

That work for me!

var $select = $('.select2').select2();
//console.log($select);
$select.each(function(i,item){
  //console.log(item);
  $(item).select2("destroy");
});
jcmordan
  • 1,038
  • 13
  • 20
5

Version 4.0

$element.data('select2').destroy();
Pang
  • 9,564
  • 146
  • 81
  • 122
Nasri Zhang
  • 63
  • 1
  • 4
1
$(".select2").select2('destroy');
$(".select2").val('Your Value');
$('.select2').each(function () {
    $(this).select2({ dropdownParent: $(this).parent() });
});
STA
  • 30,729
  • 8
  • 45
  • 59
  • While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Amit Verma Feb 06 '21 at 14:19