Vivian Guillen
Vivian Guillen

Reputation: 158

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?

Upvotes: 9

Views: 25515

Answers (2)

magnarja
magnarja

Reputation: 21

maybe items can be used..

$('select').selectize({
items : [""], // initializing selected value to nothing
plugins: ['remove_button','restore_on_backspace']
});

Upvotes: 2

Allan Bazinet
Allan Bazinet

Reputation: 1847

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");

Upvotes: 24

Related Questions