Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an item from Selectize?

Is there any way I can remove an item from Selectize?

Here is my a sample list:

  1. AMNT
  2. QTY
  3. NA

When I pass NA it should remove particular item:

   $.fn.removeSelectorValue = function (value) {
      var selectize = this[0].selectize;
      selectize.removeItem(value);

      return this;
   };

This is not working. Can anyone help me with this?

like image 603
John Thomas Avatar asked Nov 11 '14 20:11

John Thomas


People also ask

How do you empty a Selectize?

selectize(); var control = $select[0]. selectize; control. clear();

How do I remove options from a drop down in HTML?

Select remove() Method The remove() method is used to remove an option from a drop-down list. Tip: To add an option to a drop-down list, use the add() method.


2 Answers

$(document).on('click', 'div.selectize-input div.item', function(e) {
    var select = $('#services').selectize();
    var selectSizeControl = select[0].selectize;
    // 1. Get the value
    var selectedValue = $(this).attr("data-value");
    // 2. Remove the option 
    select[0].selectize.removeItem(selectedValue);

    select[0].selectize.refreshItems();
    select[0].selectize.refreshOptions();

});

This code do not remove the item from the select, just remove it from the selected options.

like image 56
M4r Avatar answered Sep 18 '22 15:09

M4r


removeItem removes selected item identified by given value. To remove option from the list you should use removeOption

Example - open http://brianreavis.github.io/selectize.js/, open console and enter:

$('#select-beast')[0].selectize.removeOption(1)

to remove Chuck Tesla from available options

like image 37
Michał Samujło Avatar answered Sep 22 '22 15:09

Michał Samujło