Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable typing on selectize

As the title, on selectize, how can I disabled typing except Backspace key.

It will be allowed to:

  • Select item on dropdown.
  • Delete selected items.

It will NOT be allowed to:

  • Type or add any new items.

I have read the API document but I can't found the solution. Any suggestions.

Here mine:

var $select = $('#tags').selectize({
        maxItems: 5,
        persist: false,
        createOnBlur: true,
        create: true,
    });

UPDATE:

I found the solution by my own

$select[0].selectize.$control_input.on('keydown', function(e) {
        var key = e.charCode || e.keyCode;
        if(key == 8 )
            return true;
        else
            e.preventDefault();
    });
like image 988
TommyDo Avatar asked Oct 05 '16 05:10

TommyDo


1 Answers

While the way you did it works, the proper way to prevent item addition is to use create: false:

var $select = $('#tags').selectize({
    maxItems: 5,
    persist: false,
    create: false
});
like image 91
basher Avatar answered Nov 03 '22 07:11

basher