select2 disable / enabled not working

Viewed 13761

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.

3 Answers

Basic jQuery seems to solve this problem quite nice:

Enable:

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

Disable:

$("#list1").attr('disabled', true);

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);

Try this for what you need

$('#list1').select2("enable",true)
Related