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
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
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);
The right way for Select2 3.x is:
$('select').select2("enable", false)
This works fine.
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);
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/
$('#foo').select2('enable', false);
in my case. –
Kiva $('#foo').select2('disable');
does not work anymore –
Merrileemerrili 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.
In selec2 site you can see options. There is "disabled" option for api. You can use like :
$('#foo').select2({
disabled: true
});
As of Select2 4.1, they've removed support for .enable
$("select").prop("disabled", true); // instead of $("select").enable(false);
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);
I'm disabling select2 with:
$('select').select2("enable",false);
And enabling it with
$('select').select2("enable");
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);
$('select').select2('enable',false);
This works for me.
I'm disable on value:
<option disabled="disabled">value</option>
if you want to disable the values of the dropdown
$('select option:not(selected)').prop('disabled', true);
$('select').prop('disabled', true);
© 2022 - 2024 — McMap. All rights reserved.