whyte624
whyte624

Reputation: 330

How to select all options in Tom-select

I want to be able to select all options or 1 optgroup in select built with TomSelect.

There is no option to select all. I reviewed documentation and could not find any info in github.

Upvotes: 1

Views: 1379

Answers (1)

mokazemi
mokazemi

Reputation: 78

I also had this problem today. You can do this like that to select all options:

    var tomSelect = new TomSelect(...)

    $('#check-all').click(function(event) {
        const allValues = tomSelect.options;
        var valuesToSelect = Object.keys(allValues).map(function(key) {
            return allValues[key].value;
        });
        tomSelect.setValue(valuesToSelect); // Set all values to select them
    });

Also this to uncheck all:

    $('#uncheck-all').click(function(event) {
        event.preventDefault();
        tomSelect.setValue([]); // Set all values to empty to uncheck all
    });

Upvotes: 2

Related Questions