Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default value in selectize.js?

I'm trying to find a way to set a default value in selectize.js.

I know that in jQuery you can use:

$(".country").val("US");

But this doesnt work with Selectize. I already read the API documentation and I tried using:

$(".country").selectize().setValue("US");

But it doesnt work either.

I'm a newbie so I'm probably missing something here but can't figure out what. Any ideas?

like image 511
Vivian Guillen Avatar asked Sep 16 '14 14:09

Vivian Guillen


2 Answers

Per the documentation, API functions are called on the selectize property that the selectize() call adds to the original select or input element; this property points to the underlying selectize instance.

Since it's a property of the element, you use the [0] syntax to the element from jQuery:

// initialize the selectize control
var $select = $('.country').selectize();

$select[0].selectize.setValue("US");
like image 186
Allan Bazinet Avatar answered Oct 31 '22 06:10

Allan Bazinet


maybe items can be used..

$('select').selectize({
items : [""], // initializing selected value to nothing
plugins: ['remove_button','restore_on_backspace']
});
like image 22
magnarja Avatar answered Oct 31 '22 05:10

magnarja