I'm using the jquery select2 plugin but I want to disable it for certain select dropdowns. Is there a way to exclude select elements the plugin will be applied to? I was thinking maybe a class on the select element to tell the plugin to ignore it.
I'm using the jquery select2 plugin but I want to disable it for certain select dropdowns. Is there a way to exclude select elements the plugin will be applied to? I was thinking maybe a class on the select element to tell the plugin to ignore it.
Share Improve this question asked May 1, 2015 at 15:36 steven35steven35 4,0176 gold badges39 silver badges49 bronze badges4 Answers
Reset to default 6$('select').not('.yourExcludeClass').select2();
Here's a link to the docs for $.not()
While just not calling .select2()
on the elements which you don't want to be dropdown works, you might want to disable Select2 after you've already initialized it.
In order to do this, you have two options
- Visually disable it like you would a standard
<select>
by calling$("select").prop("disabled", true)
. - "Destroy" Select2 so it goes back to looking like a standard dropdown by calling
$("select").select2("destroy")
.
You can disable it using -
$('select').select2("enable",false);
using JQuery.
I am assuming you are applying it to all selects via:
<script type="text/javascript">
$('select').select2();
</script>
There are a few ways you could solve it, but the easiest would be to add a class to the selects you want to be styled And only call the select2()
on those:
- Add the class
addStyling
to your selects to be styled - Initialize them with
$('select.addStyling').select2();