I want to apply select2 to a bunch of jquery elements on the page that all have the same class name but it looks like if i call select2() on an element that already has had a select2() called on it then it blows up. here is my code
$('.MyDropdowns').each(function (i, obj) { $(obj).select2({ width: "455px" }); });
so I want something like:
$('.MyDripdowns').each(function (i, obj) { if (!$(obj).HasSelect2Initiatized) { $(obj).select2({ width: "455px" }); } });
Does anything like this exist?
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.
Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.
In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();
you can check if the element has select2
attribute
$('.MyDripdowns').each(function (i, obj) { if (!$(obj).data('select2')) { $(obj).select2({ width: "455px" }); } });
EDIT
As @Fr0zenFyr said in his comment for v4.0 you can use :
if (!$(obj).hasClass("select2-hidden-accessible"))
Working solution:
$('.MyDripdowns:not([class^="select2"])').each(function (i, obj) { $(obj).select2({width: "455px"}); })
Links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With