Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 disable / enabled not working

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.

like image 897
Atakan Savaş Avatar asked May 14 '26 15:05

Atakan Savaş


2 Answers

Basic jQuery seems to solve this problem quite nice:

Enable:

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

Disable:

$("#list1").attr('disabled', true);
like image 157
Aleksandar Belic Avatar answered May 17 '26 03:05

Aleksandar Belic


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);
like image 28
WenJun Avatar answered May 17 '26 04:05

WenJun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!