How to remove an item from Selectize
Is there a way to remove an item from selectize
- AMNT
- QTY
- NA
when i pass NA it should remove a specific element
$.fn.removeSelectorValue = function (value) {
var selectize = this[0].selectize;
selectize.removeItem(value)
return this;
};
this does not work. Can anyone help me with this?
source to share
removeItem
removes the selected item identified by the given value. To remove an option from the list, you must useremoveOption
Example - open http://brianreavis.github.io/selectize.js/ , open your console and type:
$('#select-beast')[0].selectize.removeOption(1)
remove Chuck Tesla from available options
source to share
I'm late to the party, but other methods don't work for me, I don't know, because of this, because I was pulling in the list from a remote source?
In short, there are 2 steps:
- Get the value of the selected item
- Remove this item
Obviously this code is smaller, but I have left it verbose for readability.
var $select = $('#users').selectize();
var selectSizeControl = $select[0].selectize;
// 1. Get the value
var selectedValue = selectSizeControl.getValue()
// 2. Remove the option
selectSizeControl.removeOption( selectedValue )
source to share
$(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 does not remove the item from the selected, just remove it from the selected options.
source to share
This was recently implemented and if you remove the last item the input looks like an error (as mentioned above). The workaround (hacker's kind) is in the function onItemRemove
, find the length of the stored elements, and if length == 0
, use jQuery to correct the input -$('.selectize-input').css({'height':'35px'});
source to share