31

I know how to enable the selected attribute from dropdown; I can use this code :

$('select').select2();

but my problem is how to disable it ? thx Click Here

Paolo
  • 20,112
  • 21
  • 72
  • 113
Attila Naghi
  • 2,535
  • 6
  • 37
  • 59

12 Answers12

60

The right way for Select2 3.x is:

$('select').select2("enable", false)

This works fine.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
user4105545
  • 609
  • 1
  • 5
  • 2
47

For those using Select2 4.x, you can disable an individual option by doing:

$('select option:selected').prop('disabled', true);

For those using Select2 4.x, you can disable the entire dropdown with:

$('select').prop('disabled', true);
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
robert
  • 609
  • 6
  • 14
  • I edited my post, see the image. This method is not working. I think there is a specific function from select2 for this, but Im a newbie to this. – Attila Naghi Jan 17 '14 at 13:32
  • I tried this version to : $('select option:selected').attr('disabled','true'); – Attila Naghi Jan 17 '14 at 13:45
  • I'm a little confused, you want to disable the select element or an option of the select? If it is the select element try: $('select').select2().enable(false); – robert Jan 17 '14 at 13:46
  • it works for the firs time, but for the second times it disables all of the 2 dropdowns. Can I do something like this? $(' #subTasks select').select2().enable(false), where id ="subTasks" belongs to the second dropdown – Attila Naghi Jan 17 '14 at 13:59
  • $('#subTasks').select2().enable(false) => I want to write this, but it disables it completly even if I want to choose another value for Tasks,Subtask it is still disabled. I dont know if you understand me :D – Attila Naghi Jan 17 '14 at 14:06
  • In that case you have to control the disable/enable action depending on what you select in the in the Task select element. Remember that you can enable the Subtask again by doing this: $('#subTasks').select2().enable(true). – robert Jan 17 '14 at 14:13
  • this answer not work for me, in select2.js it gave me error "throw "query function not defined for Select2 " + opts.element.attr("id");" answer from user4105545 work fine – Alex Oct 17 '14 at 08:30
  • As of Select2 4.1, they've removed support for .enable: '$("select").prop("disabled", true); // instead of $("select").enable(false);' – Matt Feb 23 '18 at 09:37
  • Following this : https://github.com/select2/select2/issues/5309#issuecomment-405140337 I was doing $("select").select2().prop("disabled", false), which is WRONG. You have to enable on field directly : $("select").prop("disabled", false) or your Ajax option grabbing capabilities won't work – ZalemCitizen Feb 26 '20 at 10:36
17

The below code also works fine for Select2 3.x

For Enable Select Box:

$('#foo').select2('enable');

For Disable Select Box:

$('#foo').select2('disable');

jsfiddle: http://jsfiddle.net/DcunN/

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Anitha
  • 365
  • 5
  • 14
9

To disable the complete select2 box, that is no deletion of already selected values and no new insertion, use:

$("id-select2").prop("disabled", true);

where id-select2 is the unique id of select2. you can also use any particular class if defined to address the dropdown.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
proprius
  • 502
  • 9
  • 20
8

In selec2 site you can see options. There is "disabled" option for api. You can use like :

$('#foo').select2({
    disabled: true
});
Atakan Savaş
  • 185
  • 1
  • 3
  • 10
5

As of Select2 4.1, they've removed support for .enable

$("select").prop("disabled", true); // instead of $("select").enable(false);

From: https://select2.org/upgrading/migrating-from-35

Matt
  • 1,494
  • 2
  • 18
  • 38
3

As the question seems unclear, I'm sorry if this answer is not directly related to the original intent.

For those using Select2 version 4+ and according to official plugin documentation, .select2("enable")is not the way to go anymore for disabling the select box (not a single option of it). It will even be completely removed from version 4.1 onward.

Quoted directy from the documentation (see https://select2.org/upgrading/migrating-from-35#select2-enable):

Select2 will respect the disabled property of the underlying select element. In order to enable or disable Select2, you should call .prop('disabled', true/false) on the element. Support for the old methods will be completely removed in Select2 4.1.

So in the previous answer's example, it should be: $('select').prop(disabled,true);

Os1r1s110
  • 47
  • 1
  • 3
  • 10
3

I'm disabling select2 with:

$('select').select2("enable",false);

And enabling it with

$('select').select2("enable");
tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
Miftah Mizwar
  • 3,722
  • 2
  • 13
  • 20
3

As per select2 documentation: Click Here

If you wants to disable select2 then use this approach:

$(".js-example-disabled").prop("disabled", true);

If you wants to enable a disabled select2 box use this approach:

$(".js-example-disabled").prop("disabled", false);
Ghulam Rasool
  • 125
  • 1
  • 8
1
$('select').select2('enable',false);

This works for me.

weeraa
  • 1,123
  • 8
  • 23
  • 40
0

I'm disable on value:

<option disabled="disabled">value</option>
Alberto Cerqueira
  • 1,339
  • 14
  • 18
0

if you want to disable the values of the dropdown

$('select option:not(selected)').prop('disabled', true);

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