bluzky / nice-select2

A lightweight vanilla javascript library that replaces native select elements with customizable dropdowns
https://bluzky.github.io/nice-select2/
MIT License
370 stars 61 forks source link

How to apply for 2 or more selects in a form like state and city? #31

Closed EduVillas closed 1 year ago

EduVillas commented 2 years ago

How to apply for 2 or more selects in a form like state and city?

Tsjippy commented 1 year ago

Thats possible

mattneal-stafflink commented 1 year ago

If you want to apply it to every select element, and they're all basic dropdowns/selects (no search, etc) you can get every element and iterate over them like this:

const e = document.getElementsByTagName("select");

for (var i = 0; i < e.length; i++) {
    NiceSelect.bind(e[i]);
}

I have one select that's slightly different, so I just added a condition within it.

function updateSelectFields() {

    const e = document.getElementsByTagName("select");

    for (var i = 0; i < e.length; i++) {

        if ( e[i].classList.contains('searchable') ) {
            NiceSelect.bind(e[i], {searchable: true, placeholder: 'Search'});
        } else {
            NiceSelect.bind(e[i]);
        }

    }
}