How can I disable selected attribute from select2() dropdown Jquery?
Asked Answered
F

12

35

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

Fairleigh answered 17/1, 2014 at 13:18 Comment(1)
it is a JQuery pluginFairleigh
P
48

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);
Portable answered 17/1, 2014 at 13:23 Comment(9)
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.Fairleigh
I tried this version to : $('select option:selected').attr('disabled','true');Fairleigh
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);Portable
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 dropdownFairleigh
$('#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 :DFairleigh
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).Portable
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 fineNorge
As of Select2 4.1, they've removed support for .enable: '$("select").prop("disabled", true); // instead of $("select").enable(false);'Equitant
Following this : 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 workFresher
A
61

The right way for Select2 3.x is:

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

This works fine.

Ananna answered 3/10, 2014 at 11:5 Comment(0)
P
48

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);
Portable answered 17/1, 2014 at 13:23 Comment(9)
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.Fairleigh
I tried this version to : $('select option:selected').attr('disabled','true');Fairleigh
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);Portable
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 dropdownFairleigh
$('#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 :DFairleigh
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).Portable
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 fineNorge
As of Select2 4.1, they've removed support for .enable: '$("select").prop("disabled", true); // instead of $("select").enable(false);'Equitant
Following this : 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 workFresher
I
19

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/

Inbound answered 6/11, 2014 at 9:8 Comment(2)
To disable: it's $('#foo').select2('enable', false); in my case.Kiva
$('#foo').select2('disable');does not work anymoreMerrileemerrili
A
10

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.

Abbacy answered 21/7, 2016 at 0:6 Comment(0)
M
8

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

$('#foo').select2({
    disabled: true
});
Maquette answered 14/11, 2017 at 12:27 Comment(0)
E
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

Equitant answered 23/2, 2018 at 9:38 Comment(0)
S
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);

Stallings answered 9/11, 2017 at 16:39 Comment(0)
C
3

I'm disabling select2 with:

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

And enabling it with

$('select').select2("enable");
Cumber answered 28/3, 2018 at 7:9 Comment(0)
P
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);
Punctilious answered 28/3, 2018 at 13:48 Comment(0)
R
1
$('select').select2('enable',false);

This works for me.

Rearward answered 24/10, 2016 at 4:55 Comment(0)
C
0

I'm disable on value:

<option disabled="disabled">value</option>
Curzon answered 2/2, 2017 at 14:20 Comment(0)
C
0

if you want to disable the values of the dropdown

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

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

Claudetteclaudia answered 21/8, 2019 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.