select2 disable / enabled not working
Asked Answered
C

3

9

I have a select element with select2 plugin. Version is 4.0. It works well but i cant change disabled option dynamically.

$('#list1').select2({
    theme: "bootstrap",
    disabled: true,
    placeholder: "Parsel",
    minimumInputLength: 0,
    allowClear: true,
    closeOnSelect: true,
});

As you can see, disabled is true. I cant change disabled option dynamically. It work well if i delete disabled option.

Im using this code for try to change option;

$("#list1").select2({ disabled : false });

or

$("#list1").select2('enable');

How can i change select element's disabled option ?

Thank you all.

Curule answered 17/11, 2017 at 13:42 Comment(0)
P
13

Basic jQuery seems to solve this problem quite nice:

Enable:

$("#list1").removeAttr('disabled');

Disable:

$("#list1").attr('disabled', true);
Permute answered 13/6, 2018 at 10:49 Comment(4)
Actually, this not fix my problem. But after i change my html codes, it works well. Thanks.Capitalize
@AtakanSavaş, if you put your answer, we all would profit.Sham
@AtakanSavaş what did you change, don't leave us hangingCrandall
@TimBogdanov I dont remember at all but try to disable parent item.Capitalize
D
3

It is better to use prop instead of attr in jQuery. Use true or false to set the disabled status.

eg:

Enable dropdown - $("#list1").prop('disabled', false);
Disable dropdown - $("#list1").prop('disabled', true);
Dejesus answered 20/8, 2020 at 6:4 Comment(2)
It's not just better; that's the correct and only guaranteed way to do it using jQuery.Midland
Also note the official example for disabling a select2 control.Jarvisjary
A
0

Try this for what you need

$('#list1').select2("enable",true)
Algeciras answered 17/11, 2017 at 13:45 Comment(4)
gives an error. says : .select2(...).enable is not a functionCapitalize
ok i tried it too. this time it not give any error but nothing changed. i think there is different problem but i cant find yet.Capitalize
i think you should start your dropdown disabled directly in html <select disabled></select> not via javascriptAlgeciras
i cant change any another option. but i cant find why.Capitalize

© 2022 - 2024 — McMap. All rights reserved.