How to clear a selected value in Selectize.js dropdown?
Asked Answered
P

6

52

I have a selectize.js dropdown and I have to clear the selected value .

I have tried this (as suggested in another question):

var selectize = $("#optionNetFlow")[0].selectize;
selectize.clear();

But it gives the following error:

enter image description here


When I change it to this:

var selectize = $("#optionNetFlow").selectize;
selectize.clear();

I gives this error:

enter image description here


What I am doing wrong here?

Pavilion answered 14/11, 2014 at 5:50 Comment(3)
If you check docs mentioned in provided incorrect answer (just imagine - 9 vote ups for incorrect answer. Well, maybe other part of answer is correct, but the first code line is deadly wrong) - you will see how it has to be done.Earlie
Thanks @Regent, I got it sorted. Thanks for help.Pavilion
It happens sometimes. Sometimes without any adequate reason. Just relax. It's not that you have low reputation, you know. My vote up can compensate 2.5 downvotes.Earlie
P
82

I finally found the answer here Selectize.js Demos

What works for me is:

 var $select = $('#optionNetFlow').selectize();
 var control = $select[0].selectize;
 control.clear();

what I was missing var $select = $('#optionNetFlow').selectize(); before applying the solution provided in above question's answer.

Now I am to get all the functions in console like :

enter image description here

Pavilion answered 14/11, 2014 at 5:54 Comment(2)
This solution also removes the record(clear the dropdown list). You can no longer search in the list.Wartow
@Wartow No it doesn't. Just checked this solution - it works perfectly. I can still search and select the cleared item. Version used to check this - Selectize.js v0.12.4.Lucho
P
30

Try this,

$("#optionNetFlow")[0].selectize.clear();
Phia answered 25/8, 2017 at 9:22 Comment(3)
This is the best way, you don't have to store the selectize in a global variable when initializing, just call this whenever you need.Knisley
Best answer. And after you have cleared the input, you might need to focus it, to open list. Use for that: focus().Gainer
that's great it works for me I just added the ID of my select box and boom it worked thanks a lotBanting
C
11

Try this out:- http://jsfiddle.net/adiioo7/2gnq1ruv/204/

JS:-

jQuery(function ($) {
    var $select = $('#input-tags').selectize({
        persist: false,
        create: true
    });

    $("#btnClear").on("click", function () {
        var selectize = $select[0].selectize;
        selectize.clear();

    });
});
Chordate answered 14/11, 2014 at 6:4 Comment(3)
Thanks for help. I got sorted it out. +1 for jsfiddle.Pavilion
what does persist do ?Somersomers
@Somersomers - It says "keep option after it's been un-selected or not?" as a boolean flag, basically. Source is Usage docs: github.com/selectize/selectize.js/blob/master/docs/usage.mdConfinement
M
9

All other answers either clear a single selectize or need a specific reference to the selectize in the moment of it's creation.

The solution below, on the other hand, works for any number of selectize elements you have inside any form; you just need to specify the desired form:

$('form').find('.selectized').each(function(index, element) { element.selectize && element.selectize.clear() })

The rationale is that Selectize keeps the original element in the DOM (hiding it), adds a reference to the selectize on the .selectize property of the DOM element and adds a CSS class selectized to it.

So the solution finds all the elements that have the CSS class selectized, loops through them and calls element.selectize.clear().

Mouton answered 7/3, 2019 at 15:47 Comment(2)
+1 for this! But just a question, what does this line means? element.selectize && element.selectize.clear()Ivanna
It means: if element.selectize isn't null or undefined, then clear itBorodino
M
1
$(document).on('click', 'div.selectize-input div.item', function(e) {
    var select = $('#services').selectize();
    var selectSizeControl = select[0].selectize;
    // 1. Get the value
    var selectedValue = $(this).attr("data-value");
    // 2. Remove the option
    select[0].selectize.removeItem(selectedValue);
    // 3. Refresh the select
    select[0].selectize.refreshItems();
    select[0].selectize.refreshOptions();
});

This do not remove the item from the select, just remove it from the selected options.

Misdoubt answered 22/2, 2017 at 17:36 Comment(0)
D
1

Or if you have multi select, and do want to restore selected items in the drop-down list (hide selected set to true).

var selectize = $("#select-item").selectize;
//clone array
var items = selectize.items.slice(0);
for (var i in items) {
    selectize.removeItem(items[i]);
}
selectize.refreshOptions();
Dempster answered 18/1, 2019 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.