Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit selectize input after calling selectize()

My goal is to reset selected values in selectize input. I called selectize function in seperate js file (working with Ruby on Rails), so there is no option to create some global selectize input variable (at least I want to avoid it).

In select2 I would solve similar issue like that:

$(".select2").select2("val", "")

but when I call selectize() second time on the input all previous options and collection loaded via ajax just dissappear.

Thanks for help

like image 837
res Avatar asked Sep 28 '22 00:09

res


1 Answers

Selectize uses .selectized as its identifying class.

Unfortunately, selectize is not accessible via $('.selectized').selectize - you need to go directly to the <select>/<input> element - $('.selectized')[0].selectize as shown in the docs.

I don't believe you can access all selectize items at once, you need to iterate through them. I recommend using the map function, such as something like:

$.map($('.selectized'), function(item, index) { 
  item.selectize.setValue('');
});
like image 200
basher Avatar answered Nov 15 '22 03:11

basher