selectize.js : to clear selected value in onChange event
Asked Answered
Q

1

5

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
     }
 }
});
Quintillion answered 15/1, 2015 at 0:31 Comment(2)
do u mind deleeting some of that extra left-space? it looks good just too much to rightTacy
$('#select-with-selectize-triggered-on')[0].selectize.removeOption(value);Tref
Q
7

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 {
}
},
Quintillion answered 15/1, 2015 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.