select2 will not disable or clear
Asked Answered
H

9

13

I have a secondary select slaved to a primary select (pick a store, then pick a department - identical to pick a country, then pick a state).

I absolutely cannot get select2's ('enable',false) and ('data', null) methods to work, no matter how much other code I tear out.

<select id="stores">
  <option value="foo">foo</option>
</select>
<select id="depts">
  <option value="bar">bar</option>
</select>

// ...some logic that selects a store, then fetches that store's depts ...

$('#depts').select2('enable',false);// does not disable control
$('#depts').select2('data',null); // does not clear control

So I'm forced to do this:

$('select#depts').empty(); // clears HTML element
$('#depts').select2(); // re-enhances select w/ select2
$('#depts').select2('disable',true); // disables 

It behaves itself in jsfiddle, so I can't even post an example and request help. I'm just ... stumped.

Haggerty answered 16/7, 2013 at 15:36 Comment(0)
E
27
// to disable
$('#statusSelect').select2('disable');

//to enable
$('#statusSelect').select2('enable');
Esperance answered 10/11, 2014 at 11:33 Comment(0)
T
13

This works for me in all browsers:

$('#statusSelect').select2('destroy');
$('#statusSelect').prop('disabled', true);
$('#statusSelect').select2();

That gives you a disabled select2 input.

To re-enabled simply:

$('#statusSelect').select2('destroy');
$('#statusSelect').prop('disabled', false);
$('#statusSelect').select2();

The down side is that this causes your select box to change appearance for a split second when select2 is destroyed and reapplied.

Also, "readonly" and "disabled" are not supported in older versions of select2.

Tifanie answered 25/11, 2013 at 9:34 Comment(1)
I think reinitializing might no longer be necessary. See the official example for disabling a select2 control.Sabra
Q
5

You're probably experiencing select2 bug 1104. Unfortunately it's still a problem in IE8-10, but it's not select2's fault. The problem is that IE doesn't trigger the propertychange event when an element is disabled, and also doesn't implement the MutationObserver functionality that other browsers use. Luckily, there's a tiny jQuery plugin that I wrote that will allow the propertychange event to be fired when an element is disabled. You can find it here.

$('#originalSelect').fireOnDisable().select2();
$('#originalSelect').prop('disabled', true);

should now work in IE8-10. If you need to support IE6-7, you're on your own!

Quiteria answered 5/11, 2013 at 3:16 Comment(2)
In IE9/IE10 the disable attribute doesn't work for multile select2.Gallenz
IE8-10 don't have any attribute watcher, only property watcher - so you might be experiencing a problem where you're setting the attribute and not the property - in my tests it works fine using jquery to .prop(), but not .attr()Quiteria
T
3

If you have unique id for select2 dropdown box, using that id,

$("#id-select2").attr("disabled", true);
Trajan answered 20/7, 2016 at 22:19 Comment(0)
W
3

This code should work in all browsers (select2 v4.0.4):

To disable:

$('select').prop('disabled', true).trigger('change');

To enable:

$('select').prop('disabled', false).trigger('change');
Whencesoever answered 7/4, 2018 at 9:36 Comment(0)
S
1

For IE you have to re initialize select2 after enable/disable

// change action disable
$('.custom-select').select2()
.on("change", function(e) {
   $('.custom-select').prop("disabled", true).select2(); //call select2
})

// disable alternative
 $('.custom-select').prop("disabled", true).select2(); //call select2
Saleable answered 4/2, 2016 at 17:25 Comment(0)
M
0

would you not require this:

$('#depts').prop('disabled', false);

same as this question

Mend answered 16/7, 2013 at 15:40 Comment(1)
This question is not the same as the one you linked since it uses select2 plugin, it is not a simple select to be disabled.Twana
W
0

If anyone is trying to do it in .net server code:

this.mySelect2elementID.Attributes.Add("disabled", "true");
Wellrounded answered 22/3, 2017 at 16:5 Comment(0)
C
0

Just Use the trigger event

$('#depts').val('').trigger('change');
Cheesy answered 29/5, 2023 at 7:23 Comment(1)
Thanks. Now I can finally finish this project from 2013. :DHaggerty

© 2022 - 2024 — McMap. All rights reserved.