Disabling selectize dropdown when a checkbox is checked
Asked Answered
R

4

28

I have implemented Selectize on my HTML form. However a dropdown only becomes active when the "enable" checkbox is clicked. I know there is a disable property on the Selectize object but I dont know how to use it when the checkbox is clicked.

I have tried adding the disabled class to the Selectize div element but that does not work either.
Any help will be well appreciated.

Thanks

Ramadan answered 2/12, 2014 at 19:3 Comment(0)
O
52

I wanted to add an additional answer here because although @tclark333's answer is correct, it's a bit misleading since the first line is the actual initialization of the selectize object, and not actually what's needed for the answer.

The selectize API is exposed when you access the selectize property on the underlying element from a jQuery object and not as an extension to jQuery itself.

Assuming the element that has been selectized's ID is "myDropDown":

//disable
$('#myDropDown')[0].selectize.disable();
//re-enable
$('#myDropDown')[0].selectize.enable(); 
Onaonager answered 13/6, 2016 at 20:5 Comment(0)
H
32

It's a little weird how you have to set it up. Here's what works for me.

var select = $("#YourDropDownId").selectize();
var selectize = select[0].selectize;
selectize.disable();
Hydraulic answered 8/1, 2015 at 15:40 Comment(0)
P
2

This method put automatic locked class to each selectize if parent is readonly (use your own logic to put readonly on select)

$('.custom-select-2').each(function(){
    $(this).selectize({
         create: true,
         sortField: {
             field: 'text',
             direction: 'desc'
         }
    });
    if ($(this).is('[readonly]')) {
        $(this)[0].selectize.lock();
    }
})

after you can customize the select with this css

select[readonly]{
    pointer-events: none;
    background-color: #e9ecef;
}
.selectize-input.items.full.has-options.has-items.locked {
    background-color: #e9ecef;
}

Result:

example with bootstrap

Playsuit answered 4/7, 2020 at 2:22 Comment(0)
T
0
function generateCircle(id , jPath){
        var formId =$("#"+id).closest(".data_details_accord").find("form");
        var select = formId.find("select");
        /*disable select initially*/ 
        select.each(function(){
            var thisSelect = $(this).selectize();
            thisSelectDisable = thisSelect[0].selectize;
            thisSelectDisable.disable();
        });

        /***********/ 

        $.ajax({
            url: jPath,
            data:formVlaz,
            success: function(result){

            },error: function (xhr , status, eror) {
            },complete: function (xhr) {

                /*enable select when ajax complete*/ 
                    select.each(function(){
                        var thisSelect = $(this).selectize();
                        thisSelectDisable = thisSelect[0].selectize;
                        thisSelectDisable.enable();
                    });

                /********/ 
            }
        });
    };
Tarim answered 21/2, 2017 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.