How to set a default value in selectize.js?
Asked Answered
C

2

9

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?

Chabot answered 16/9, 2014 at 14:6 Comment(0)
I
24

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");
Iridissa answered 22/9, 2014 at 3:10 Comment(1)
if i am adding new option using var $select =$('#dept').selectize({ create: true, sortField: 'text' }); then how to set its value.Bunnell
R
2

maybe items can be used..

$('select').selectize({
items : [""], // initializing selected value to nothing
plugins: ['remove_button','restore_on_backspace']
});
Ronrona answered 2/7, 2021 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.