javascript - How to add class to select2 element in v4.0 -


this similar question on how add class select2 element, answers there appear target earlier versions of framework has undergone breaking changes in v4.0

according issue add additional custom class select2-container, there several undocumented properties pass select2 constructor, including: containercss, containercssclass, dropdowncss, , dropdowncssclass.

however in version 4 when run following code:

$('.select2').select2({     containercss: "wrap" }); 

i following error:

uncaught error: no select2/compat/containercss

how can pass class select2 in v4.0?

here's example in stacksnippets:

$('.select2').select2({      containercss: "wrap"  });
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>  <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>    <select class="select2 narrow wrap">    <option value="al">algebra</option>    <option value="ak">alaska</option>    <option value="az">arizona</option>  </select>

select2 store information uses on original select element. can access following information calling .data('select2') on original element:

select2 data

from there, can access $container property identify container in dom , add class this:

var $select2 = $('select.select2').select2()  $select2.data('select2').$container.addclass("wrap") 

here's demo in stack snippets

var $select2 = $('select.select2').select2()  $select2.data('select2').$container.addclass("wrap")
body .select2.narrow {      width: 200px;  }  body .wrap .select2-selection--single {      height: 100%;  }  body .select2-container.wrap .select2-selection--single .select2-selection__rendered {      word-wrap: break-word;      text-overflow: inherit;      white-space: normal;  }
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>  <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.full.js"></script>    <select class="select2 narrow wrap">    <option value="al">really long name normally</option>    <option value="ak">alaska</option>    <option value="az">arizona</option>  </select>


Comments

Popular posts from this blog

python - Creating a new virtualenv gives a permissions error -

facebook - android ACTION_SEND to share with specific application only -

go - Idiomatic way to handle template errors in golang -