selectize.js : to clear selected value in onChange event

ashkash17 picture ashkash17 · Jan 15, 2015 · Viewed 8.1k times · Source

I am trying to clear out the value selected in the onChange event depending on a condition. Whether or not its already present in a selected list. However i am not able to clear of the value using
$select[name][0].selectize.clear(); from inside the onChange object. I tried various versions of it , like using the elements id. Please help me with this.

$select[name].selectize({
valueField: 'lot_name',
labelField: 'lot_name',
create: true,
options: res,
render: {
    option: function (item, escape) {
         return '<div>' +
             '<span><strong>' + item.lot_name + '</strong></span>' +
             '</div>';
     },
     option_create: function (data, escape) {
      return '<div class="create">Map to <strong>' + escape(data.input) + '</strong>&hellip;</div>';
     }
 },
onChange: function (value) {
     if($.inArray(value, selectedList) > -1){
         alert("Lot " + value + " has already been selected. Please recheck your selection and  try again");
    $select[name][0].selectize.clear();
     } else {
          // else logic
     }
 }
});

Answer

ashkash17 picture ashkash17 · Jan 15, 2015

This is the solution I found.

     /* Using the Below inside onChange event handler
      var obj = $(this);
      obj[0].setValue("");
     */


onChange: function (value) {
 var obj = $(this);
 if (($.inArray(value, selectedList) > -1)) {
   alert("Lot " + value + " has already been selected. Please recheck your selection and try again");
   obj[0].setValue("");
 } else {
}
},