Choices-js / Choices

A vanilla JS customisable select box/text input plugin ⚡️
https://choices-js.github.io/Choices/
MIT License
6.24k stars 615 forks source link

Multiple Select Fields will dont working #1038

Closed JDittmerCOM closed 2 years ago

JDittmerCOM commented 2 years ago

I have two everything works, but with the second only a simple list is shown.

How can I fix this to be the same as the first field?

Choices.js Version 10.1.0

Bild_2022-05-22_145231377

MrMooky commented 2 years ago

I just stumbled onto this as well. My code looks like this:

if (document.getElementsByTagName('select').length >= 1) {
    new Choices('select', {
        allowHTML: false,
        searchEnabled: false,
    });
}

Only the first select on the page is working. I'm also not sure what to make of this note in the docs:

Note: If you pass a selector which targets multiple elements, the first matching element will be used. Versions prior to 8.x.x would return multiple Choices instances.

To me it basically sounds like that for each select there should be a new Choices instance created manually? That would be bad.

Denisbeder commented 2 years ago

Same here.

My solution for the moment


const items = document.querySelectorAll("[data-select]");

items.forEach((item) => new Choices(item));
JDittmerCOM commented 2 years ago

Thank you for the answers.

However, I found a solution for myself a long time ago.

if($('#choicesjs-gameserver-permissions')[0]){
    new Choices('#choicesjs-gameserver-permissions',
        {
            removeItemButton: true,
            searchEnabled: true,
            itemSelectText: '',
            allowHTML: true
        }
    );
}